`

理解java中的JNDI:comp/env/jdbc/datasource 和 jdbc/datasource 的區別

阅读更多

Understanding JNDI in the java: comp / env / jdbc / datasource and jdbc / datasource difference

 

 

[Original] understanding of JNDI in the java: comp/env/jdbc/datasource and jdbc/datasource difference. 

 

In describing the JNDI, such as access to the data source, JNDI address two written, for example, the same jdbc / testDS data source: 

A: java: comp / env / jdbc / testDS 

B: jdbc / testDS 

 

Both versions, the configuration of different ways, first Fangfa should be regarded as a kind of familiar applications of Fangfa transplantation or transfer, its realization and the "mapping", the concept of Er B method, he is a hard reference. 

java: comp / env is the environment naming context (environment naming context (ENC)), is in the EJB 1.1 specification introduced after the introduction of the JNDI lookup to resolve the original conflict arising, but also to improve the EJB or J2EE application migration sex. 

In the J2EE reference in common are: 

JDBC data source reference in the java: comp / env / jdbc sub-context statement 

JMS connection factories in the java: comp / env / jms child in the context of a statement 

JavaMail connection factories in the java: comp / env / mail sub-context statement 

URL connection factories in the java: comp / env / url child in the context of a statement 

 

Indicated by the following structure to find that the difference between the two descriptions: 

A: java: comp / env / jdbc / testDS (virtual address) ------> mapping descriptor ------> jdbc / testDS (actual address) 

B: jdbc / testDS (actual address) 

From this structural point of view, A is indeed easy to transplant. 

 

Look at an example: 

If you need to get datasource, such as: dataSource = (DataSource) ctx.lookup ("java: comp / env / jdbc / testDS"); 

Then in the configuration file for resource mapping in web.xml, 

 

<resource-ref> 
   <res-ref-name> jdbc / testDS </ res-ref-name> 
   <res-type> javax.sql.DataSource </ res-type> 
   <res-auth> Container </ res-auth> 
</ Resource-ref> 

 

 

Xml in the appropriate allocation of resources (the different application servers are different, WSAD, you can visualize the settings), 

 

<reference-descriptor> 
   <resource-description> 
     <res-ref-name> jdbc/DBPool </ res-ref-name> 
     <jndi-name> OraDataSource </ jndi-name> 
   </ Resource-description> 
</ Reference-descriptor> 

 

 

JNDI name of the actual server is OraDataSource, logical name jdbc / DBPool and it is only used for mapping, so the advantage is to increase portability, portable configuration files should only change when what you can, but the application can not change. 

 

If you write a general application, would like to get directly through the JNDI data source that directly lookup ("mytest") on Can the (if the server JNDI to mytest), but will use the first error in the wording . 

################################################## ############################# 

 

java: comp / env is the standard J2EE environment to use this approach to find rules of the environment have to do a mapping of names to the JNDI name of this isolation makes the writing program, do not concern the real JNDI name has bluntly put configuration with the JNDI name file is the same usage as follows, if the java: comp / env / my / datasource mapped to my.ora.dataource 

 

web.xml: 

 

<resource-ref>
   <res-ref-name> My / datasource </ res-ref-name>
   <res-type> Javax.sql.DataSource </ res-type>
   <res-auth> CONTAINER <res-auth>
</ Resource-ref>

  

 

weblogic.xml 

 

<reference-descriptor> 
  <resource-description> 
  <res-ref-name> my/datasource </ res-ref-name> 
  <jndi-name> my.ora.dataource </ jndi-name> 
...... 

 

 

Instead of using the prefix is actually a direct JNDI name 

 

------------------------- 

 

Add no additional time is global JNDI name, this will result in application of the coupling between the EJB too high, not recommended 

################################################## ############################# 

ENC concept: 

 

The application component environment is referred to as the ENC, the enterprise naming context.

Application component business logic should be obtained ENC object. Component provider uses the standard deployment descriptor specify the required ENC entrance. The entrance is a run-time components of ENC rely on resources and other information. 

An application component instance using the JNDI Location ENC. ENC standard JNDI CONTEXT is: java:comp/env 

 

/ / Obtain the application component's ENC 
Context iniCtx = new InitialContext (); 
Context compEnv = (Context) iniCtx.lookup ("java:comp/env"); 

 

env environment is a private environment, can only access the components inside. On the other components are not visible. For example, EJB1 can not access EJB2 the ENV. Similarly, any client code, whether it is running on application servers or within the same JVM is a remote call, and can not access the JNDI. In this case, the component is isolated between the different components can be defined between the environmental parameters of its own. For example EJB1 can define your own environment variable parameters: java: comp / env / red 

 

1, JBOSS namespace: 

For example, a name: java: comp / env all bind java: name of the following sub-environment, are only able to access the internal server JBOSS. Can not remote access, such as DataSource were bound in the java: below. Can not remote access. The EJB, JTA were bound in the global scope, to remote access. 

 

Some of the usual ENV: 

 

ENV carried out with the env-entry stated. 

 

ejb references with ejb-ref, ejb-local-ref to declare. 

Resource management connection factory resource-ref to affirm with. 

Resources and the environment variable references with the resource-env-ref to declare. 

 

1.1 Environment Entries 

Examples: 

 

<session> 
  <ejb-name> ASessionBean </ ejb-name> 
  <! - ... -> 
  <env-entry> 
    <description> The maximum number of tax exemptions allowed </ description> 
    <env-entry-name> maxExemptions </ env-entry-name> 
    <env-entry-type> java.lang.Integer </ env-entry-type> 
    <env-entry-value> 15 </ env-entry-value> 
  </ Env-entry> 
  <env-entry> 
    <description> The tax rate </ description> 
    <env-entry-name> taxRate </ env-entry-name> 
    <env-entry-type> java.lang.Float </ env-entry-type> 
    <env-entry-value> 0.23 </ env-entry-value> 
  </ Env-entry> 
</ Session> 

 

 

 

ENC env-entry access code fragment :

InitialContext iniCtx = new InitialContext (); 

Context envCtx = (Context) iniCtx.lookup ("java: comp / env"); 

Integer maxExemptions = (Integer) envCtx.lookup ("maxExemptions"); 

Float taxRate = (Float) envCtx.lookup ("taxRate"); 

 

1.2 EJB Reference 

 

In the development process, and sometimes need to reference each other between the EJB. The references are generally to be carried out under the JNDI name, but the JNDI name in deployment can decide. Therefore need a means of reference in the deployment of another EJB, ejb-reference is to meet this requirement. 

ejb reference is an application component naming environment that points to a deployed EJB HOME link. J2EE specification recommends that all such links should then be organized in the java: / comp / env / ejb namespace. 

 

<session> 
<ejb-name> ShoppingCartBean </ ejb-name> 
<! - ...--> 
</ Session> 

<session> 
<ejb-name> ProductBeanUser </ ejb-name> 
<!--...--> 
<ejb-ref> 
<description> This is a reference to the store products entity </ description> This attribute optional 
<ejb-ref-name> ejb / ProductHome </ ejb-ref-name> 
<ejb-ref-type> Entity </ ejb-ref-type> can choose Entity and Session 
<home> org.jboss.store.ejb.ProductHome </ home> 
</ Ejb-ref> 
<remote> org.jboss.store.ejb.Product </ remote> 
</ Session> 

<session> 
<ejb-name> ShoppingCartUser </ ejb-name> 
<!--...--> 
<ejb-ref> 
<ejb-ref-name> ejb / ShoppingCartHome </ ejb-ref-name> 
<ejb-ref-type> Session </ ejb-ref-type> 
<home> org.jboss.store.ejb.ShoppingCartHome </ home> 
<remote> org.jboss.store.ejb.ShoppingCart </ remote> 
<ejb-link> ShoppingCartBean </ ejb-link> 
<! - Point in the same EJB JAR or an APPLICATION UNIT with a EJB, and the name of the EJB. -> 
</ Ejb-ref> 
</ Session> 

<entity> 
<description> The Product entity bean </ description> 
<ejb-name> ProductBean </ ejb-name> 
<!--...--> 
</ Entity> 

 

 

ejb-ref within the scope of this paragraph attributes stated EJB components. This means that other application components at runtime can not access these properties. Other components of the definition of the same name will not reference the name of the conflict. 

 

InitialContext iniCtx = new InitialContext (); 
Context ejbCtx = (Context) iniCtx.lookup ("java: comp / env / ejb"); 
ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup ("ShoppingCartHome"); 

 

 

JBOSS reference method: 

 

<session> 
<ejb-name> ProductBeanUser </ ejb-name> 
<ejb-ref> 
  <ejb-ref-name> ejb/ProductHome </ ejb-ref-name>
corresponding ejb-jar.xml file in the ejb-ref-name element 
  <jndi-name> jboss/store / ProductHome </ jndi-name> 
</ Ejb-ref> 
</ Session> 

<entity>
<ejb-name> ProductBean </ ejb-name> 
<jndi-name> jboss / store / ProductHome </ jndi-name> 
<! - ... -> 
</ Entity> 

 

 

As can be seen from the above, if the ejb / ProductHome reference to be re-directed to the jndi-name: jboss / store / ProductHome 

 

1.3 EJB Local Reference 

 

EJB 2.0 added ejb local reference, and thus from the perspective of semantics to ensure that the references to local references, but not by way of RMI. 

 

<session> 
<ejb-name> Probe </ ejb-name> 
<home> org.jboss.test.perf.interfaces.ProbeHome </ home> 
<remote> org.jboss.test.perf.interfaces.Probe </ remote> 
<local-home> org.jboss.test.perf.interfaces.ProbeLocalHome </ local-home> 
<local> org.jboss.test.perf.interfaces.ProbeLocal </ local> 
<ejb-class> org.jboss.test.perf.ejb.ProbeBean </ ejb-class> 
<session-type> Stateless </ session-type> 
<transaction-type> Bean </ transaction-type> 
</ Session> 
<session> 
<ejb-name> PerfTestSession </ ejb-name> 
<home> org.jboss.test.perf.interfaces.PerfTestSessionHome </ home> 
<remote> org.jboss.test.perf.interfaces.PerfTestSession </ remote> 
<ejb-class> org.jboss.test.perf.ejb.PerfTestSessionBean </ ejb-class> 
<session-type> Stateless </ session-type> 
<transaction-type> Container </ transaction-type> 
<ejb-ref> 
<ejb-ref-name> ejb / ProbeHome </ ejb-ref-name> 
<ejb-ref-type> Session </ ejb-ref-type> 
<home> org.jboss.test.perf.interfaces.SessionHome </ home> 
<remote> org.jboss.test.perf.interfaces.Session </ remote> 
<ejb-link> Probe </ ejb-link> 
</ Ejb-ref> 
<ejb-local-ref> 
<ejb-ref-name> ejb / ProbeLocalHome </ ejb-ref-name> 
<ejb-ref-type> Session </ ejb-ref-type> 
<local-home> org.jboss.test.perf.interfaces.ProbeLocalHome </ local-home> 
<local> org.jboss.test.perf.interfaces.ProbeLocal </ local> 
<! - EJB local interface of the fully qualified name -> 
<ejb-link> Probe </ ejb-link> 
</ Ejb-local-ref> 
</ Session> 

 

 

Access syntax: 

 

InitialContext iniCtx = new InitialContext (); 
Context ejbCtx = (Context) iniCtx.lookup ("java: comp / env / ejb"); 
ProbeLocalHome home = (ProbeLocalHome) ejbCtx.lookup ("ProbeLocalHome"); 

 

 

1.4 Resource manaager connection factory reference 

 

By a series of resource-ref form. Each child node reference to the following: 

 

description (optional) 

res-ref-name: java: / comp / env name related to the following 

res-type: the type of resources, the resource manager connection factory's fully qualified name. 

res-auth: resources, rights management needs. Two types: Application or container 

res-share-scope (optional): does not support current JBOSS. 

 

J2EE specification recommends that all resources and references are organized in the java: / comp / env name space below, and each type of resource has its own sub-namespace. 

 

For example: JDBC DataSource Reference should be declared in the java: / comp / env / jdbc subContext. 

 

JMS connection factories should be declared int the java: / comp / env / jms subContext. 

 

A reference document in WEB.XML example: 

 

<web> 
<! - ... -> 
<servlet> 
<servlet-name> AServlet </ servlet-name> 
<! - ... -> 
</ Servlet> 
<! - ... -> 
<! - JDBC DataSources (java: comp / env / jdbc) -> 
<resource-ref> 
<description> The default DS </ description> 
<res-ref-name> jdbc / DefaultDS </ res-ref-name> <! - the name must file with the following description of the line -> 
<res-type> javax.sql.DataSource </ res-type> 
<res-auth> Container </ res-auth> 
</ Resource-ref> 
<! - JavaMail Connection Factories (java: comp / env / mail) -> 
<resource-ref> 
<description> Default Mail </ description> 
<res-ref-name> mail / DefaultMail </ res-ref-name> 
<res-type> javax.mail.Session </ res-type> 
<res-auth> Container </ res-auth> 
</ Resource-ref> 
<! - JMS Connection Factories (java: comp / env / jms) -> 
<resource-ref> 
<description> Default QueueFactory </ description> 
<res-ref-name> jms / QueueFactory </ res-ref-name> 
<res-type> javax.jms.QueueConnectionFactory </ res-type> 
<res-auth> Container </ res-auth> 
</ Resource-re> 

 

 

Program for reference: 

 

Context initCtx = new InitialContext (); 
javax.mail.Session s = (javax.mail.Session) 
initCtx.lookup ("java: comp / env / mail / DefaultMail"); 

 

jboss-web.xml (corresponding to WEB), jboss.xml (corresponding ejb) document provides res-ref-name of the JNDI name of the conversion. 

 

This is done by the jboss.xml or jboss-web.xml file to provide resource-ref completed. 

 

For example: 

<jboss-web> 
<! - ... -> 
<resource-ref> 
<res-ref-name> jdbc / DefaultDS </ res-ref-name> 
<res-type> javax.sql.DataSource </ res-type> 
<jndi-name> java: / DefaultDS </ jndi-name> <! - will be redirected to the JNDI resource reference above -> 
</ Resource-ref> 
<resource-ref> 
<res-ref-name> mail / DefaultMail </ res-ref-name> 
<res-type> javax.mail.Session </ res-type> 
<jndi-name> java: / Mail </ jndi-name> 
</ Resource-ref> 
<resource-ref> 
<res-ref-name> jms / QueueFactory </ res-ref-name> 
<res-type> javax.jms.QueueConnectionFactory </ res-type> 
<jndi-name> QueueConnectionFactory </ jndi-name> 
</ Resource-ref> 
<! - ... -> 
</ Jboss-web> 

 

Resource Environment References to provide a resource to use their own resources. 

<session> 
<ejb-name> MyBean </ ejb-name> 

<resource-env-ref> 
<description> This is a reference to a JMS queue used in the 
processing of Stock info 
</ Description> 
<resource-env-ref-name> jms / StockInfo </ resource-env-ref-name> 
<resource-env-ref-type> javax.jms.Queue </ resource-env-ref-type> 
</ Resource-env-ref> 
<! - ... -> 
</ Session> 

 

In the program visit: 

InitialContext iniCtx = new InitialContext (); 
javax.jms.Queue q = (javax.jms.Queue) 
envCtx.lookup ("java: comp / env / jms / StockInfo"); 

 

Similarly, specific platform needed to provide resource-env-ref-name to the JNDI of the conversion, such as JBOSS.xml file: 

 

<session> 
  <ejb-name> MyBean </ ejb-name> 
  <resource-env-ref> 
  <resource-env-ref-name> Jms / StockInfo </ resource-env-ref-name> 
  <jndi-name> Queue / StockInfoQueue </ jndi-name> This is a true global JNDI name 
  </ Resource-env-ref> 
  <! - ... -> 
  </ Session>

 

分享到:
评论
1 楼 daAoBaMapigu 2014-08-27  
亲哥 装b用英语。。小心被雷劈呀

相关推荐

    在Weblogic上配置Hibernate为JNDI

    这里,`java:comp/env/hibernate/session_factory`是JNDI查找的名称,它对应于hibernate.properties中配置的`hibernate.session_factory_name`,即使在Weblogic中实际名称需要以点号分隔。 通过以上步骤,我们成功...

    java jndi的使用

    Java JNDI(Java Naming and Directory Interface)是Java平台中用于访问命名和目录服务的一组API。它允许Java开发者在应用程序中查找和绑定资源,如数据源、EJB(Enterprise JavaBeans)、邮件服务器等。JNDI的核心...

    tomcat6.0连接通过JNDI连接mysql实例

    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/JNDIDatabase"); conn = ds.getConnection(); } return conn; } ``` 在上面的代码中,我们使用 `InitialContext` 对象来查找 JNDI 资源,并从中...

    JNDI简单应用示例

    Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的一组API。它提供了一种标准的方法来查找和绑定数据,这些数据可以是对象引用、配置信息或者其他资源。JNDI通常与Java应用程序...

    JNDI配置文件

    Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的API,它为各种服务提供了一个统一的接口,如数据库连接、邮件服务等。在Java应用程序中,JNDI常用于查找和绑定资源,如数据源、...

    在Tomcat下配置JNDI.doc

    在这里,`&lt;property name="connection.datasource"&gt;java:comp/env/jdbc/quickstart&lt;/property&gt;`告诉Hibernate使用JNDI名称`java:comp/env/jdbc/quickstart`来查找数据源。 最后,将`hibernate.cfg.xml`文件放入Web...

    websphere上发布基于jndi的应用

    这里的`"java:comp/env/jdbc/MyDataSource"`就是之前在部署描述符中定义的JNDI名称。 5. **测试与调试**:部署应用后,进行功能测试以确保应用能正确地通过JNDI找到并使用资源。如果遇到问题,可以查看WebSphere的...

    javaJNDI连接数据库

    2. **查找数据源**:使用`context.lookup("java:comp/env/jdbc/news")`方法根据之前在`context.xml`中定义的数据源名称查找相应的`DataSource`对象。 3. **获取连接**:通过`getConnection()`方法从`DataSource`中...

    MSSQL 2008 连接java jdbc详解

    这里,`java:comp/env/jdbc/yourDataSource`是数据源在JNDI中的名称,需要在服务器配置文件中对应设置。 在所有这些方法中,使用微软的JDBC驱动方法二通常被认为是性能最好的,因为它直接与SQL Server通信,而无需...

    在tomcat6.0.18下手动配置jndi

    - 注意,这里的`java:comp/env/jdbc/myDataSource`是JNDI查找路径,`java:comp/env`是标准的Java EE环境命名空间,`jdbc/myDataSource`则是我们在`context.xml`中定义的名称。 4. **测试配置**: - 重启Tomcat...

    Java Jndi数据连接池,sql2000或者sql2005数据库

    DataSource source = (DataSource) ic.lookup("java:comp/env/jdbc/jndidemo"); dbcon = source.getConnection(); 需要导的包 import javax.naming.Context; import javax.naming.InitialContext; import ...

    j2ee JNDI 存储 和 读取程序 weblogic

    在Java EE(以前称为J2EE)环境中,Java Naming and Directory Interface (JNDI) 是一个重要的服务,它提供了一种标准的方式来查找和访问各种命名和目录服务。JNDI 主要用于在分布式环境中查找和绑定资源,如数据源...

    在Weblogic9.2里添加JDBC连接,并用JNDI名称进行调用

    这里的`java:comp/env/jdbc/MyDataSource`是JNDI查找路径,其中`jdbc/MyDataSource`对应我们在WebLogic管理控制台中定义的JNDI名称。 6. **注意事项**: - 确保JDBC驱动与数据库版本兼容。 - 配置数据源时,避免...

    JavaJNDI 数据链接池及 属性文件读取数据链接信息

    JavaJNDI(Java Naming and Directory Interface)是Java平台中用于访问命名和目录服务的一组API,它允许Java应用程序查找和管理分布在不同网络环境中的资源。数据连接池(Connection Pool)是Java应用中用于管理...

    Tomcat jndi 配置数据源过程

    这里的`java:comp/env/jdbc/MyDB`是JNDI查找路径,其中`java:comp/env`是标准的Java EE容器上下文,`jdbc/MyDB`是从`context.xml`和`web.xml`中定义的JNDI名称。 总结,通过上述步骤,你已经在Tomcat中成功配置了一...

    在weblogic 10 中配置jndi数据源

    这里的`"java:comp/env/jdbc/MyDataSource"`是JNDI名称,与在WebLogic中配置的名称相对应。 7. **源码和工具** - 标签提到的“源码”可能指的是应用中引用JNDI数据源的Java代码,而“工具”指WebLogic控制台或...

    JNDI 三种连接数据库方法

    DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/MyDB"); Connection conn = ds.getConnection(); ``` 请注意,这种方法需要对服务器环境有深入的理解,并且配置路径可能会因服务器不同而变化。 总结...

    Tomcat下Teradata的JNDI数据源配置

    而JNDI则是Java平台中的一个标准服务,提供了一种查找和访问各种命名和目录服务的方式,例如数据源、邮件服务器等。 在Tomcat中配置Teradata的JNDI数据源,我们需要遵循以下步骤: 1. **准备驱动**: Teradata的...

    Java通用Dao包括JDBC和JNDI两种连接方式

    2. 查找数据源:DataSource dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/yourDataSourceName")。 3. 获取连接:Connection conn = dataSource.getConnection()。 4. 使用连接并关闭:执行...

    数据池连接Name jdbc is not bound in this Context解决方案

    总之,解决“Name jdbc is not bound in this Context”错误的关键在于正确配置数据源,并在Java代码中使用JNDI查找数据源以获取数据库连接。理解并熟练掌握这些步骤对于开发基于Java Web的应用程序来说至关重要。

Global site tag (gtag.js) - Google Analytics