I recently read the usage of the Java database connection pool, and I have seen the usage of a function like lookup, which generally includes:
(Javax. SQL. datasource) CTX. Lookup ("Java: COMP/ENV/XXX ")
(Javax. SQL. datasource) CTX. Lookup ("XXX ")
At first, I thought this was the same, but in fact it was quite different:
Java: COMP/ENV is different from JNDI. Java: COMP/ENV is the environment naming context (Environment naming context (ENC), which was introduced after EJB specification 1.1, this is introduced to solve conflicts caused by the original JNDI lookup, and to improve the portability of EJB or J2EE applications. ENC is a reference used to locate enterprise applications.ProgramThe logical name of the external resource. References are defined in the application deployment descriptor file. During deployment, reference the physical location (JNDI name) bound to the resources in the target operable environment ). ENC is used to free up the hard encoding of the JNDI lookup for other resources. By configuring this referenceCodeTo point to different EJB (JNDI ).
Commonly used references in J2EE include:
--------- The JDBC data source is referenced in the Java: COMP/ENV/jdbc subcontext.
--------- The JMS connection factory declares in the Java: COMP/ENV/JMS subcontext
--------- Javamail connection factory declares in the Java: COMP/ENV/mail subcontext
--------- URL Connection factory declares in the Java: COMP/ENV/URL subcontext
Assume that you have written an EJB to obtain datasource such as datasource = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/dbpool ");
In the ejb-jar.xml, When you map resources in the configuration file,
<Resource-ref>
<Res-ref-Name> JDBC/dbpool </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
In the weblogic-ejb-jar.xml,
<Reference-Descriptor>
<Resource-description>
<Res-ref-Name> JDBC/dbpool </RES-ref-Name>
<JNDI-Name> oradatasource </JNDI-Name>
</Resource-description>
</Reference-Descriptor>
// Reporter Note: if it is in JBoss, make the following changes in JBoss. xml:
<Resource-managers>
<Resource-Manager>
<Res-Name> JDBC/dbpool </RES-Name>
<Res-JNDI-Name> oradatasource </RES-JNDI-Name>
</Resource-Manager>
</Resource-managers>
The actual server's JNDI name is oradatasource, and the logical name JDBC/dbpool is only used to map to it. The advantage of doing so is to improve portability, during migration, you only need to change the configuration file, but the application does not need to be changed.