JNDI study concluded (1) - JNDI Introduction to

JNDI is the Java Naming and Directory Interface (Java Naming and Directory Interface), is an important specification in the J2EE specification, many experts believe that without a thorough understanding of the significance and role of JNDI, it did not really grasp the knowledge of J2EE EJB especially .
So, JNDI in the end what role?

To understand the role of JNDI, we can learn from. "If we do not have JNDI what to do? After using JNDI What will we do?" Question to discuss.

1. No JNDI approach

When programmers, know to develop applications to access the MySQL database and, therefore, a reference to the MySQL JDBC driver class is encoded, and connect to the database by using the appropriate JDBC URL.
The following code is like this:

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 the conn and SQL operations * /
   
   conn.close();
}
catch(Exception e) {
   e.printStackTrace ();
}
finally {
   if(conn!=null) {
     try {
       conn.close();
     } catch(SQLException e) {}
   }
}

This is the traditional approach, but also before non-Java programmers (such as Delphi, VB, etc.) is common practice. This practice is generally small-scale development process will not be a problem, as long as the programmers familiar with Java, JDBC technology to understand and MySQL, can quickly develop the appropriate application.

JNDI is no question of the existence of the practice:
1, the database server name MyDBServer, user name and password may need to change, triggered JDBC URL needs to be modified;
2, the database may switch to other products, such as switch DB2 or Oracle, triggered JDBC driver package and class name needs to be modified;
3, with the increase in the actual use of the terminal, connection pool parameters of the original configuration may need to be adjusted;
4, ......

solution:
the programmer should not need to be concerned about "specific database What background? What JDBC driver is? What JDBC URL format? What user name and password to access the database is? "and so these problems, programmers should not write drivers for JDBC referenced, not the server name, no user name or password - or even no database connection pool management. But these issues to the J2EE container configuration and management, the programmer only needs to configure and manage these can be referenced.

As a result, there will be a JNDI.

2. Use the JNDI practice after

First, disposed in a J2EE container JNDI parameters, define a data source, i.e. JDBC reference parameters, the data source to set a name; then, in the program, to access the data source referenced by the background database data source name.
Specific operation is as follows (in JBoss example):
1, a data source arranged
in the JBoss D: "jboss420GA" docs "examples " jca folder, there are many different data sources referenced by the database defined templates. Will be one of mysql-ds.xml Copy files to the server you're using, if for D: "jboss420GA" server "default " deploy.

Modify the contents of mysql-ds.xml file to make it right through JDBC access to your MySQL database, as follows:

<?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, the definition of a data source named MySqlDS, which parameters include the URL of JDBC, driver class name, user name and password. 

Method Tomcat configuration:


<context path="/tudu" docbase="/home/web/Tudu &gt;&lt;br /&gt;&lt;Resource name=" auth="Container" type="javax.sql.DataSource">

<resourceparams name="jdbc/tudu">

<parameter>

<name>username</name>

<value>system</value>

</parameter>

<parameter>

<name>password</name>

<value>manager</value>

</parameter>

<parameter>

<name>driverClassName</name>

<value>com.mysql.jdbc.Driver</value>

</parameter>

<parameter>

<name>url</name>

<value>jdbc:mysql://localhost:3306/tudu</value>

</parameter>

</resourceparams>

</context>


2, the data source referenced in the program:

Connection conn=null;

try {

   Context ctx=new InitialContext();

   Object datasourceRef = ctx.lookup ( "java: MySqlDS"); // references a data source

   DataSource ds=(Datasource)datasourceRef;

   conn=ds.getConnection();

   / * Use conn database SQL operations * /

   

   c.close();

}

catch(Exception e) {

   e.printStackTrace ();

}

finally {

   if(conn!=null) {

     try {

       conn.close();

     } catch(SQLException e) { }

   }

}

Or by directly using JDBC JNDI data source reference programming code amount is almost the same, but now the program, without regard for the specific parameter JDBC.
After deployment, if the relevant parameters of the database changes, only need to reconfigure mysql-ds.xml modify the JDBC parameters, as long as the data source of the same name, the program source code without modification.

Thus, to avoid a tight coupling between the JNDI program and database, make the application easier configuration, easy to deploy.

JNDI extension:
JNDI meet the requirements of the underlying data source arranged on the further expansion of the effect: All references to external systems and resources, and can be defined by reference JNDI.

Therefore, in the J2EE specification, J2EE resources are not limited to JDBC data sources. There are many types of references, including resource references (already discussed), environmental entities and EJB references. In particular EJB references, it exposes Another key role of JNDI in J2EE: Find other application components.

EJB JNDI reference is very similar to JDBC resource references. Service tends to be converted in the environment, this is a very effective method. It should carry out configuration management for all components of the application architecture obtained from the EJB component to JMS queues and topics, to the simple configuration string or other objects, which can reduce maintenance costs, service changes with the passage of time, resulting , but also can simplify deployment, reduce integration work. External Resources "


Summary:
J2EE specification requires that all J2EE containers must provide JNDI implementation specification. JNDI in J2EE role is to "switch" - J2EE components at run time to find common ground mechanism other components, resources, or services. In most cases, provide JNDI provider of container can serve as a limited data storage, so that administrators can set the application's execution properties, and for other applications refer to these attributes (Java Management Extensions (Java Management Extensions, JMX) also can be used for this purpose). The main role of JNDI in the J2EE application is to provide a layer of indirection, so that components can find the resources they need, without having to understand these indirect.

In J2EE, JNDI is a J2EE application together a binder, JNDI provides indirect addressing to allow across the enterprise to deliver scalable, powerful and flexible applications. This is the promise of J2EE, and after some pre-planning and consideration, this commitment is entirely achievable.

Reproduced in: https: //my.oschina.net/zhanghaiyang/blog/593295

Guess you like

Origin blog.csdn.net/weixin_33766805/article/details/92656229