After reading this, I understand jndi.

Source: Internet
Author: User
Tags domain name server jboss

What is NDI?

Jndi is a Java naming and directory interface (Java naming and directory Interface), which is one of the important specifications in the EE specification, and many experts believe that without a thorough understanding of the significance 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:

  1. Connection conn=null;
  2. try {
  3. Class.forName ("Com.mysql.jdbc.Driver",
  4. True, Thread.CurrentThread (). Getcontextclassloader ());
  5. Conn=drivermanager.getconnection ("Jdbc:mysql://mydbserver?user=qingfeng&password=mingyue");
  6. /* Use conn and perform SQL operations */
  7. ......
  8. Conn.close ();
  9. }
  10. catch (Exception e) {
  11. E.printstacktrace ();
  12. }
  13. finally {
  14. if (conn!=null) {
  15. try {
  16. Conn.close ();
  17. } catch (SQLException e) {}
  18. }
  19. }

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, the database server name mydbserver, user name and password may need to change, resulting in the JDBC URL needs 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 、......


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:

  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <datasources>
  3. <local-tx-datasource>
  4. <jndi-name>mysqlds</jndi-name>
  5. <connection-url>jdbc:mysql://localhost:3306/lw</connection-url>
  6. <driver-class>com.mysql.jdbc.driver</driver-class>
  7. <user-name>root</user-name>
  8. <password>rootpassword</password>
  9. <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.mysqlexceptionsorter< /exception-sorter-class-name>
  10. <metadata>
  11. <type-mapping>mysql</type-mapping>
  12. </metadata>
  13. </local-tx-datasource>
  14. </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:

  1. Connection conn=null;
  2. try {
  3. Context ctx=New InitialContext ();
  4. Object datasourceref=ctx.lookup ("Java:mysqlds"); //reference data source
  5. DataSource ds= (DataSource) datasourceref;
  6. Conn=ds.getconnection ();
  7. /* Database SQL operation with Conn */
  8. ......
  9. C.close ();
  10. }
  11. catch (Exception e) {
  12. E.printstacktrace ();
  13. }
  14. finally {
  15. if (conn!=null) {
  16. try {
  17. Conn.close ();
  18. } catch (SQLException e) {}
  19. }
  20. }

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.

Jndi extensions: 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 ".


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.

Recently has been on the notes of the Java EE and review, although the Java EE video has been seen once, but when I look at the notes I made is still very strange, and really know a concept of the time is to take notes and previously seen the video impression of friction, the main content of the Java EE is the specification , it is clear that some of the concepts, at this stage of the goal is not to master the Java EE, but the outline and conceptual understanding and understanding of the Java EE, to the next step in the DRP project to the various norms of friction and understanding.

Jndi, translated to the Java Naming and directory structure (javanaming and directory Interface) is officially interpreted as jndi as a set of APIs that access the naming and directory services in a Java application (applicationprogramming Interface) Description is very refined, but more abstract.

The above explanation improves both the naming service and the directory service concepts. To understand Jndi first you must know what the naming service and directory service are for.

The more effective way to learn new concepts and knowledge is by contacting and comparing with what you have learned in the past.

About the naming service, in fact, we are often used it, but do not know it is it, more typical is the DNS domain name server (naming service), large-to-human DNS or relatively understand, It is a service that maps a domain name to an IP address. For example, Baidu's domain name the IP address mapped is, You enter two content in the browser to the same page. The reason for naming servers is because we remember that Baidu's meaningful letters are easier to remember than, but if you stand on the computer's point of view, it prefers to handle these numbers.

There are a lot of similar examples in our lives, such as your ID and your name can be "understood" into a naming service, and your school number and name can also be "interpreted" as a naming service.

You can see the features of the naming service: a mapping of one value to another, and a one by one mapping of values that are easier for us to recognize than computers that are easier to recognize.

Should you understand the naming service now?

As for directory services, from a computer perspective to the Internet has a variety of resources and hosts, but these are scattered in the Internet, in order to access these scattered resources and access to the corresponding services, you need to use directory services.

From our daily life to understand the concept of directory services can be said from the phone book, the phone book itself is a more typical directory services, if you want to find someone's phone number, you need to find the name of the person in the phone book, and then look at his phone number.

Understanding naming Services and directory services look back, Jdni, an application interface that provides naming services for Java applications, gives us a common, unified interface for locating and accessing various naming and directory services. Through the Jndi unified interface We can access a variety of different types of services. As shown, We can access the DNS that we just talked about through the Jndi API.

Now that there is a preliminary understanding of Jndi, if you want to learn more about Jndi, and the convenience of using JDNI, I recommend two articles about Jdni, written very good, two articles from "If not jndi what do we do?" What will we do after using Jndi? "This perspective deepens the understanding of Jndi.

After reading this, I understand 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: 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.