Understand the differences between Java: COMP/ENV/jdbc/datasource and JDBC/datasource in JNDI

Source: Internet
Author: User

To describe JNDI, for example, when obtaining a data source, the JNDI address can be written in either of the following ways:
A: Java: COMP/ENV/jdbc/testds
B: JDBC/testds

The configuration methods are also different. The first method is a method that facilitates program transplantation or migration. Its implementation is the same as the concept of "ing, method B is a hard reference.
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.
Commonly used references in J2EE include:
The JDBC data source is referenced in the Java: COMP/ENV/jdbc subcontext.
The JMS connection factory is declared in the Java: COMP/ENV/JMS subcontext
Javamail connection factory declares in the Java: COMP/ENV/mail subcontext
The URL Connection factory is declared in the Java: COMP/ENV/URL subcontext.

The following structure diagram shows the differences between the two descriptions:
A: Java: COMP/ENV/jdbc/testds (Virtual Address) ------> ing descriptor ------> JDBC/testds (actual address)
B: JDBC/testds (actual address)
From this structure, A is indeed easy to transplant.

Let's look at an example:
Assume that you need to obtain datasource, for example, datasource = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/testds ");
When you map resources in the configuration file, 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>
In the corresponding resource configuration XML (different application servers are different, and you can perform visual settings in wsad ),
<Reference-Descriptor>
<Resource-description>
<Res-ref-Name> JDBC/dbpool </RES-ref-Name>
<JNDI-Name> oradatasource </JNDI-Name>
</Resource-description>
</Reference-Descriptor>
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.

If you write a general application and want to directly obtain the data source through JNDI, you can directly Lookup ("mytest") (if the JNDI on the server is mytest ), if you use the first method, an error is returned.

 

Related Article

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.