理解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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值