Http://blog.csdn.net/zhaosg198312/archive/2009/03/11/3979435.aspx
JNDIJava Naming and Directory Interface is an important specification in the J2EE specification. Many experts believe that they have not thoroughly understood the significance and role of JNDI, no real knowledge of J2EE, especially EJB.
So what is the role of JNDI?
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:
ProgramWhen developers know that they want to develop applications that access the MySQL database, they encode a reference to the MySQL JDBC Driver Class and connect to the database by using the appropriate jdbc url.
Just like the followingCodeIn this way:
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 ){}
}
}
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.
Problems with the absence of JNDI:
1. Database Server NameMydbserver, The user name and password may need to be changed, and thus the jdbc url needs 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:
<? 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>
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:
Connection conn = NULL;
Try {
Context CTX = new initialcontext ();
Object export ceref = 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 ){}
}
}
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 programSource codeNo modification is required.
It can be seen that JNDI avoids the tight coupling between the program and the database, making the application easier to configure and deploy.
JNDI extension:
In addition to meeting the data source Configuration Requirements, JNDI further expands its role: All references to resources outside the system can be defined and referenced through JNDI.
Therefore, in J2EE specifications, resources in J2EE are not limited to JDBC data sources. There are many types of references, including resource references (discussed), Environment entities, and EJB references. In particular, EJB references expose another key role of JNDI in J2EE: Find other application components.
The JNDI reference of EJB is very similar to the reference of JDBC resources. In an environment where services tend to be converted, this is a very effective method. This type of configuration management can be performed on all components in the application architecture, from EJB components to JMS queues and topics, to simple configuration strings or other objects, this can reduce the maintenance costs incurred by service changes over time, simplify deployment, and reduce integration work. External resources ".
Summary:
The J2EE specification requires that all J2EE containers provide the implementation of the jndi specification. The role of JNDI in J2EE is "Switch"-a general mechanism for J2EE components to find other components, resources, or services at runtime. In most cases, the container that provides the JNDI supplier can act as a limited data storage, so that the administrator can set the execution attribute of the application, and let other applications reference these attributes (Java Management Extensions (JMX) can also be used for this purpose ). The primary role of JNDI in J2EE applications is to provide an indirect layer, so that components can discover the required resources without understanding these indirect properties.
In J2EE, JNDI is the binder that combines J2EE applications. The indirect addressing provided by JNDI allows cross-enterprise delivery of scalable, powerful, and flexible applications. This is the commitment of J2EE, and after some planning and pre-consideration, this commitment can be fully implemented.