Jndi--java Naming and Directory interface __java

Source: Internet
Author: User
Tags domain name server jboss

Original address: http://sishuok.com/forum/blogPost/list/1186.html


What is JNDI?

Jndi is the 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 there is no thorough understanding of the significance and role of Jndi, There is no real grasp of Java, especially EJB knowledge.

So, what does Jndi really do.

To understand the role of Jndi, we can do this from "if we don't need jndi." What do we do when we use Jndi? "This question is to be explored.

No Jndi practices:

As programmers develop, they know that they want to develop access to the MySQL database, then 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 SQL Operations/... conn.close ();   catch (Exception e) {e.printstacktrace ();   finally {if (conn!=null) {try {conn.close (); The catch (SQLException e) {}}}

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

Problems with no Jndi practices:

1, the database server name MyDBServer, username and password may need to change, which triggers the JDBC URL needs to be modified;

2, the database may switch to other products, such as using DB2 or Oracle, to trigger the JDBC driver package and 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 adjust;

4 、......

Solution:

Programmers should not be concerned about what the specific database backend is. 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 should write a program that doesn't have a reference to the JDBC driver, no server name, no user name or password--not even a database pool or connection management." Instead, these problems are given to the Java container for configuration and management, and the programmer only needs to refer to these configurations and management.

From this, there is a jndi.

After using the JNDI approach:

First, configure the Jndi parameter in the Java EE container, define a data source, that is, a jdbc reference parameter, set a name for the data source, and then, in the program, reference the data source through the data source name to access the background database.

The details are as follows (take JBoss for example):

1. Configure the data source

Under the D:/jboss420ga/docs/examples/jca folder of JBoss, there are many data source definition templates for different database references. Copy the Mysql-ds.xml files 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 access your MySQL database correctly 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, we define a data source named Mysqlds, whose parameters include the JDBC URL, driver class name, username and password, and so on.

2, in the program to refer to the data source: Connection Conn=null;   Try {context ctx=new initialcontext (); Object datasourceref=ctx.lookup ("Java:mysqlds");   Reference data source DataSource ds= (DataSource) datasourceref;   Conn=ds.getconnection ();   /* Use conn for database SQL Operations/... c.close ();   catch (Exception e) {e.printstacktrace ();   finally {if (conn!=null) {try {conn.close (); The catch (SQLException e) {}}}

The amount of programming code that uses JDBC directly or through a Jndi reference data source is almost the same, but now the program does not have to worry about the specific JDBC parameters.
After the system is deployed, if the related parameters of the database change, only need to reconfigure Mysql-ds.xml to modify the JDBC parameters, so long as the data source name is guaranteed unchanged, then the program source code need not be modified.

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

Jndi Extensions: Jndi is further augmented by the need to meet the requirements of the data source configuration: All references to resources outside the system can be defined and referenced through Jndi.

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

A JNDI reference to an EJB is very similar to a reference to a JDBC resource. This is a very effective method in an environment where services tend to transition. This configuration management can be done for all the components that are available in the application architecture, from EJB components to JMS queues and themes to simple configuration strings or other objects, which can reduce the maintenance costs of service changes over time, while simplifying deployment and reducing integration efforts. External resources ".

Summary

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

In Java EE, Jndi is the glue that puts the Java application together, and the indirect addressing provided by Jndi allows scalable, powerful, and flexible applications to be delivered across the enterprise. This is the promise of Java EE, and after some planning and prior consideration, this commitment is fully achievable.

Recently has been in the notes to the Java EE Collation and review, although the Java-ee video is seen once, but when I look at the notes I made the unfamiliar degree is still very large, and the real understanding of a certain concept is the note and the previous saw the video impression of friction, the main explanation of the content is each specification , and then clearly some of the concepts, the current stage of the goal is not to master the Java EE, but to the outline and concept of Java EE Understanding and understanding, to the next step of the DRP project in a deeper level of friction and understanding of the various norms.

Jndi, translated to Java naming and directory structures (javanaming and directory Interface) is officially interpreted as jndi as a set of APIs for accessing naming and directory services in Java applications (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 need to know what the naming service and directory service are for.

The most effective way to learn new concepts and knowledge is by contacting and comparing them with what they have learned before.

About the naming service, in fact, we often use it, but do not know it is it, more typical domain name server DNS (domains naming service), large to the DNS or more understand, It is a service that maps domain names to IP addresses. For example, Baidu's domain name www.baidu.com mapped IP address is http://202.108.22.5/, You enter two content in the browser to the same page. The reason to use naming servers is because we remember that some of the meaningful letters of Baidu are easier to remember than remembering 202.108.22.5, but if you are on the computer's side, it prefers to handle these numbers.

There are many similar examples from our lives, such as your ID number and your name can be "understood" as a naming service, and your school number and name can be "interpreted" as a naming service.

You can see the characteristics of the naming service: a mapping of a value and another value, mapping one by one of values that are more easily known to us than those of a computer.

Should the naming service be understood by now?

As for directory services, from a computer perspective to have a wide range of resources and hosts on the Internet, but these content is scattered in the Internet, in order to access these scattered resources and access to appropriate services, need to use directory services.

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

Understand the naming service and directory service look back at Jdni, an application interface that provides naming services for Java applications. Provides us with 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 in the following illustration, we can access the DNS that we just talked about through the Jndi API.

Now that you have a rudimentary understanding of jndi, if you want to learn more about Jndi and what's convenient for us to use Jdni, I recommend two articles about Jdni, very well written, two articles from "If we don't have to Jndi." What do we do when we use Jndi? "This angle to deepen the understanding of Jndi."

Related Article

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.