What is JNDI?

Source: Internet
Author: User
Tags jboss

JNDIis the Java Naming and directory interface (Java naming and directory Interface), is one of the important specifications in the EE specification, many experts believe that without a thorough understanding of the meaning and role of Jndi, There is no real mastery of the knowledge of the Java EE, especially EJB.
So, what does Jndi really do?

to understand the role of Jndi, we can start with "What if we don't use Jndi?" What will we do after using Jndi? "This question to be explored.

There is no jndi approach:
when programmers develop, they know that to develop an application that accesses 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 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 the traditional practice, but also the previous non-Java programmers (such as Delphi, VB, etc.) common practice. This approach generally does not create problems in small-scale development processes, so long as programmers are familiar with the Java language, understand JDBC technology, and MySQL, they can quickly develop the appropriate application.

There is a problem with the practice of Jndi:
1. Database server nameMyDBServer, the user name and password may need to be changed, which causes the JDBC URL to be modified;
2, the database may switch to another product, such as the use of DB2 or Oracle, triggering the JDBC driver package and the class name needs to be modified;
3, with the actual use of the terminal increase, the original configuration of the connection pool parameters may need to be adjusted;
4 、......

Workaround:
programmers should not be concerned about "what is a specific database background?" What is the JDBC driver? What is the JDBC URL format? What is the user name and password to access the database? "And so on, programmers writing programs should have no reference to the JDBC driver, no server name, no user name or password--not even a database pool or connection management. Instead, these issues are given to the Java EE container for configuration and management, and the programmer only needs to reference these configurations and management.

Thus, there is the Jndi.

Use the following jndi approach:
First, configure the Jndi parameter in the Java EE container, define a data source, which is the JDBC reference parameter, set a name for the data source, and then, in the program, reference the data source with the data source name to access the background database.
here's how to do it (for example, JBoss):
1. Configure the data source
Under JBoss's D:/jboss420ga/docs/examples/jca folder, there are many data source definition templates that are 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 it can properly access your MySQL database through JDBC, 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, a data source named Mysqlds is defined, and its parameters include the JDBC URL, driver class name, user name and password, and so on.

2. Reference the data source in the program:

Connection Conn=null;
try {
Context ctx=new InitialContext ();
Object datasourceref=ctx.lookup ("Java:mysqlds"); Reference data source
DataSource ds= (DataSource) datasourceref;
Conn=ds.getconnection ();
/* Database SQL operation with Conn */
......
C.close ();

catch (Exception e) {
E.printstacktrace ();

finally {
if (conn!=null) {
try {
Conn.close ();
} catch (SQLException e) {}
}
}

The amount of programming code that directly uses JDBC or refers to a data source through Jndi is similar, but now the program can not care about the specific JDBC parameters.
after the system deployment, if the relevant parameters of the database change, only need to reconfigure Mysql-ds.xml to modify the JDBC parameters, as long as the name of the data source is not changed, then the program source code does not need to be modified.

Thus, Jndi avoids the tight coupling between the program and the database, making the application easier to configure and easy to deploy.

extension of Jndi:
Jndi, which satisfies the requirements of the data source configuration, further expands the role: all references to resources outside the system can be defined and referenced through Jndi.

therefore, in the Java EE specification, the resources in the Java EE are not limited to the JDBC data source. There are many types of references, including resource references (discussed already), environment entities, and EJB references. In particular, EJB references, which expose another key role of JNDI in the Java EE: Find other application components.

the JNDI reference of an EJB is very similar to a reference to a JDBC resource. In an environment where services tend to be transformed, this is a very effective approach. This kind of configuration management can be done on all the components in the application architecture, from EJB components to JMS queues and topics, to simple configuration strings or other objects, which reduces the maintenance costs of service changes over time, while simplifying deployment and reducing integration efforts. External resources ".


Summary:
The Java EE specification requires that all Java EE containers provide the implementation of the JNDI specification. The role of JNDI in the Java EE is a common mechanism for "switch"--J2EE components to find other components, resources, or services at run time grounding. In most cases, the container that provides the JNDI provider can act as a limited data store so that the administrator can set the execution properties of the application and have other applications reference those properties (Java Management Extensions, JMX) can also be used for this purpose. The primary role of JNDI in the Java EE application is to provide an indirection layer so that the components can discover the resources they need without knowing the indirection.

in Java EE, Jndi is the glue that puts the Java EE application together, and Jndi provides indirect addressing that allows scalable, powerful, and flexible applications to be delivered across the enterprise. This is the commitment of the Java EE, and after some planning and pre-consideration, this commitment is fully achievable.


What is 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.