JNDI interpretation), jndi Interpretation

Source: Internet
Author: User

Interpretation of JNDI

What is NDI?

JNDI is a Java Naming and Directory Interface (Java Naming and Directory Interface). It is one of the important specifications in the J2EE specification. Many experts believe that they do not fully understand the significance 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 refer to "What should we do without JNDI? After using JNDI, what will we do ?" This issue is discussed.

Without JNDI:

When developing, programmers know that they want to develop applications that access the MySQL database. Therefore, they encode a reference to the MySQL JDBC Driver Class and connect to the database by using the appropriate jdbc url.
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 a traditional practice and is also a common practice for non-Java programmers (such as Delphi and VB. In general, this method will not cause problems in small-scale development. As long as programmers are familiar with the Java language, understand the JDBC technology and MySQL, they can quickly develop corresponding applications.

Problems with the absence of JNDI:

1. The database server name MyDBServer, user name, and password may need to be changed, resulting in the jdbc url needing to be modified;

2. The database may use another product. For example, if you use DB2 or Oracle, The JDBC driver package and class name must be modified;

3. As the number of terminals in use increases, the original connection pool parameters may need to be adjusted;

4 ,......

Solution:

Programmers do not need to worry about "what is the specific database background? What is the JDBC driver? What is the jdbc url format? What is the user name and password used to access the database ?" And so on, the program compiled by the programmer should not reference the JDBC driver, no server name, no user name or password-or even no database pool or connection management. Instead, the problem is assigned to the J2EE container for configuration and management. The programmer only needs to reference the configuration and management.

As a result, JNDI is available.

After using JNDI:

First, configure the JNDI parameter in the J2EE container, define a data source, that is, the JDBC reference parameter, and set a name for the data source. Then, in the program, use the data source name to reference the data source to access the background database.

The procedure is as follows (using JBoss as an example ):

1. Configure the data source

Under the D:/jboss420GA/docs/examples/jca folder of JBoss, there are many data source definition templates 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 you can access your MySQL database correctly through JDBC, as shown below:

  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. Its parameters include jdbc url, Driver Class Name, user name, and password.

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 the data source
  5. DataSource ds = (Datasource) datasourceRef;
  6. Conn = ds. getConnection ();
  7. /* Use conn to perform database SQL operations */
  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 for directly using JDBC or referencing a data source through JNDI is almost the same, but the current program does not need to care about specific JDBC parameters.
After the system deployment, if the database parameters change, you only need to reconfigure the mysql-ds.xml to modify the JDBC parameters, as long as the data source name remains unchanged, then the program source code does not need to be modified.

It can be seen that JNDI avoids the tight coupling between the program and the database, making the application easier to configure and deploy.

Expansion of JNDI: On the basis of satisfying the data source Configuration Requirements, JNDI is further expanded: 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 references (discussed), Environment entities, and EJB references. 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. This type of configuration management can be performed on all components in the application architecture, from EJB components to JMS queues and topics, to simple configuration strings or other objects, this can reduce the maintenance costs incurred by service changes over time, simplify deployment, and reduce 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 runtime. In most cases, the container that provides the JNDI supplier can act as a limited data storage, so that the administrator can set the execution attribute of the application, and let other applications 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 allows cross-enterprise delivery of scalable, powerful, and flexible applications. This is the commitment of J2EE, and after some planning and pre-consideration, this commitment can be fully implemented.

I have been sorting out and reviewing J2EE notes recently. Although I have read the J2EE video, I am still familiar with reading my notes, when I really understand a certain concept, I rubbed my notes with my previous video impressions. The main content of J2EE is the specifications, and then I am clear about the concepts, at this stage, the goal is not to master J2EE, but to understand and understand the outlines and concepts of J2EE. In the next step, the DRP project will have a deep understanding of the various specifications.

JNDI, translated into Java Naming And Directory structure (JavaNaming And Directory Interface) the official explanation for it is that JNDI is a group of APIS for accessing the naming And Directory Service in Java applications (ApplicationProgramming Interface) the description is refined, but abstract.

The above explanation improves the two concepts of Naming Service and directory service. First, we must know what the Naming Service and directory service are.

To learn new concepts and knowledge, a more effective way is to associate and compare with what you have learned before.

We often use the Naming Service, but we don't know it as a typical Domain Name Server DNS (Domain Naming Service ), DNS is a service that maps domain names to IP addresses. for example, if the IP address mapped to the Baidu domain www.baidu.com is http: // 202.108.22.5/, you can enter the same page in the browser. the reason for naming the server is that it is easier to remember baidu words than to remember 202.108.22.5, But it prefers to process these numbers from a computer perspective.

There are many similar examples from our lives. For example, your ID card number and your name can be "Understood" as a naming service, your student ID and name can also be "interpreted" as a naming service.

We can see the characteristics of the Naming Service: The ing between one value and another value maps the values that we humans know more easily with those that computers Know more easily.

Should I understand the Naming Service now?

As for the directory service, it is understood from the computer perspective that there are various resources and hosts on the Internet, but these contents are all scattered on the Internet, to access these scattered resources and obtain the corresponding services, you need to use the directory service.

To understand the concept of directory service in our daily life, we can start with the phone book. The phone book itself is a typical directory service. If you want to find a person's phone number, you need to find the name of the person in the phone book and check the phone number again.

 

After understanding the Naming Service and directory service, let's look back at JDNI, which is an application interface that provides the Naming Service for Java applications, provides a universal and unified interface for searching and accessing various naming and directory services. through the unified JNDI interface, we can access various types of services. as shown in, we can access the DNS just mentioned through the jndi api.

Now I have a preliminary understanding of JNDI. If you want to learn more about JNDI and what convenience does JDNI bring to us, I recommend two articles on JDNI, I wrote very well. The two articles refer to "How can we do this without using JNDI? After using JNDI, what will we do ?" In this way, we can deepen our 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.