13 J2EE specifications (1) JNDI

Source: Internet
Author: User

Recently I was studying J2EE. I thought J2EE was also a high-level language similar to j2se, C #, and VB. I didn't expect that when I learned the video, I realized that other languages are not like this.

J2EE is a set of technical architecture completely different from traditional application development. It contains many components, which can simplify and standardize the development and deployment of application systems, thereby enhancing portability, security, and reuse value. In short, it is a set of standards!

The difference between the J2EE component and the "standard" Java class is that it is assembled in a J2EE application and has a fixed format and complies with the J2EE specifications, and is managed by the J2EE server. The J2EE specification defines J2EE components as follows: the client application and Applet are components running on the client; Java Servlet and Java Server Pages (JSP) are Web components running on the server; the enterprisejava Bean (e jb) component is 13 components (specifications) running on the server ).

Today, let's talk about its JNDI component.

I. What is JNDI?

JNDI-Java Naming and Directory Interface (Java Naming and Directory Interface) is a group of APIs that access naming and directory services in Java applications.

Naming Service, to put it bluntly, provides management of a name-key-value pair, that is, a key-value pair. A key represents the name of a resource, and a value represents the real address of the resource, the naming service allows you to find the corresponding object or resource by a unique name. In this way, the program only needs to know the name of a resource and can access it through JNDI, without the real physical address of the resource. This is a bit like a DNS service. DNS resolves a domain name to an IP address, so that you only need to enter a unique website name (that is, a domain name) in your browser to access the website, you do not need to remember the real IP address of the website.

Directory Service also provides a public resource management service. Directory Service is a special type of database that organizes and stores various public resources according to a certain data structure, such as a tree structure. The difference between such a special database and a traditional relational database is that it optimizes the query, and its data structure allows you to quickly find the desired resources, which guarantees a fast search capability, however, this design also sacrifices the efficiency of other aspects, for example, its update efficiency is much lower.

The key-value pairs managed in the directory service are also named. However, the key-value pairs have hierarchies, such as a tree, that is, a name or a name with a hierarchy, you can locate a subtree, not just an attribute. It can be seen that the directory service extends the concept of the Naming Service to provide a hierarchical information library. A directory service usually has a naming service, but a Naming Service does not have to have a directory service.

To understand the role of JNDI, we can refer to "What should we do without JNDI? After using JNDI, what will we do ?" This issue is discussed.

Without JNDI: When a programmer develops an application that needs to be developed to access the MySQL database, the programmer encodes a reference to the MySQL JDBC Driver Class, connect to the database by using the appropriate jdbc url.

Like the following code:

<Span style = "font-size: 14px;"> connection conn = NULL; try {class. forname ("com. mySQL. JDBC. driver ", true, thread. currentthread (). getcontextclassloader (); Conn = drivermanager. getconnection ("JDBC: mysql: // mydbserver? User = Qingfeng & Password = Mingyue ");/* use conn and perform SQL operations */...... conn. close ();} catch (exception e) {e. printstacktrace ();} finally {If (Conn! = NULL) {try {conn. Close () ;}catch (sqlexception e) {}}</span>

This is a traditional practice and is also a common practice for non-Java programmers (such as Delphi and VB. In general, this method will not cause problems in small-scale development. As long as programmers are familiar with the Java language, understand the JDBC technology and MySQL, they can quickly develop corresponding applications. Let's take a look at the issues.

1. The database server name mydbserver, user name, and password may need to be changed, resulting in the jdbc url needing to be modified;

2. The database may use another product. For example, if you use DB2 or Oracle, The JDBC driver package and class name must be modified;

3. As the number of terminals in use increases, the original connection pool parameters may need to be adjusted;

4 ,......

Solution:

Programmers do not need to worry about "what is the specific database background? What is the JDBC driver? What is the jdbc url format? What is the user name and password used to access the database ?" And so on, the program compiled by the programmer should not reference the JDBC driver, no server name, no user name or password-or even no database pool or connection management. Instead, the problem is assigned to the J2EE container for configuration and management. The programmer only needs to reference the configuration and management.

As a result, JNDI is available.


After using JNDI:

First, configure the JNDI parameter in the J2EE container, define a data source, that is, the JDBC reference parameter, and set a name for the data source. Then, in the program, use the data source name to reference the data source to access the background database.
The procedure is as follows (using JBoss as an example ):
1. Configure the data source
Under the D:/jboss420ga/docs/examples/JCA folder of JBoss, there are many data source definition templates referenced by different databases. Copy the mysql-ds.xml file to the server you are using, such as D:/jboss420ga/Server/default/deploy. Modify the contents of the mysql-ds.xml file so that you can access your MySQL database correctly through JDBC, as shown below:

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><datasources><local-tx-datasource>    <jndi-name>MySqlDS</jndi-name>    <connection-url>jdbc:mysql://localhost:3306/lw</connection-url>    <driver-class>com.mysql.jdbc.Driver</driver-class>    <user-name>root</user-name>    <password>rootpassword</password><exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>    <metadata>       <type-mapping>mySQL</type-mapping>    </metadata></local-tx-datasource></datasources></span>
Here, a data source named mysqlds is defined. Its parameters include jdbc url, Driver Class Name, user name, and password.

2. reference the data source in the program:

<Span style = "font-size: 14px;"> connection conn = NULL; try {context CTX = new initialcontext (); object datasourceref = CTX. lookup ("Java: mysqlds"); // reference the data source datasource DS = (datasource) datasourceref; Conn = Ds. getconnection ();/* use conn to perform database SQL operations */...... c. close ();} catch (exception e) {e. printstacktrace ();} finally {If (Conn! = NULL) {try {conn. Close () ;}catch (sqlexception e) {}}</span>
The amount of programming code for directly using JDBC or referencing a data source through JNDI is almost the same, but the current program does not need to care about specific JDBC parameters. After the system deployment, if the database parameters change, you only need to reconfigure the mysql-ds.xml to modify the JDBC parameters, as long as the data source name remains unchanged, then the program source code does not need to be modified.

It can be seen that JNDI avoids the tight coupling between the program and the database, making the application easier to configure and deploy.




13 J2EE specifications (1) JNDI

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.