JNDIJava Naming and Directory Interface is an important specification in the J2EE specification. Many experts think that they have not thoroughly understood the meaning 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 learn how to do it from the assumption that we do not need JNDI? After using JNDI, what will we do ?" This issue is discussed.
Without JNDI: When the program was developed, he knew that he wanted to develop the MySQL database application. So he encoded a reference to the MySQL JDBC Driver Class and connected to the database by using the appropriate jdbc url. Like the following code:
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 that is not used by Java programmers (such as Delphi and VB. This method will not cause problems in the small-scale development process. It only requires the programmer to be familiar with the Java language, understand the JDBC technology and MySQL, and be able to quickly develop corresponding applications.
Problems with the absence of JNDI: 1. The database server name mydbserver, username, and password may need to be changed, resulting in changes to the jdbc url; 2. The database may use another product. For example, if you use DB2 or Oracle, The JDBC driver package and class name must be changed; 3. As the terminal is added, the number of connections in the original connection pool may need to be adjusted; 4 ,......
Solution: Programmers should not have to worry about "what is the detailed database background? What is the JDBC driver? What is the jdbc url format? What is the username and password of the database ?" And so on, the program compiled by the programmer should not reference the JDBC driver, no server name, no real username or password-or even no database pool or connection management. Instead, these problems are handed over to the J2EE container for configuration and management. Programmers only need to reference these configurations and management.
As a result, JNDI is available.
After using JNDI: First, configure the number of JNDI partitions in the J2EE container, define a data source, that is, the number of partitions referenced by JDBC, and set a name for the data source. Then, in the program, the data source is referenced by the data source name to ask the background database. Perform the following operations in detail (using JBoss as an example ): 1. Configure the data source Under the Directory D:/jboss420ga/docs/examples/JCA of JBoss, there are many data source definition templates referenced by different databases. Copy the mysql-ds.xml file in to the server you are using, such as D:/jboss420ga/Server/default/deploy. Modify the content of the mysql-ds.xml file so that you can correctly ask your MySQL database through JDBC, for example: <? 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. The number of dimensions includes the jdbc url, Driver Class Name, username, 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 today's programs don't have to worry about the detailed JDBC volume. After the system deployment, assuming that the database related to the number of changes, only need to configure the number of jdbc changes in the mysql-ds.xml again, just to ensure that the name of the data source remains unchanged, then the program source code does not need to be changed.
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: On the basis of satisfying 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 reference (discussed), Environment entity, and EJB reference. 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. You can use all components in the program architecture to perform such configuration management, from EJB components to JMS queues and topics, to simple configuration strings or other objects, this can reduce the maintenance cost caused by service changes over time, simplify the deployment at the same time, and reduce the 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 the execution time. In most cases, containers that provide the JNDI supplier can act as limited data storage, so that the administrator can set the execution attribute of the application, and allows other applications to 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 agrees to deliver scalable, powerful, and flexible applications across enterprises. This is a commitment of J2EE, which can be fully implemented after some planning and pre-consideration. |