Java lookup usage

Source: Internet
Author: User
Tags jboss

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.