Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation of Java using JNDI to connect to the database?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what is the implementation of Java using JNDI to connect to the database, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Project background

In the SQL Server database used in the project itself, it is necessary to access the Sybase database under some functions (things that have been eliminated, QAQ). Considering that there are few functions and our UAT and PROD environment uses WebSphere, which already has the connection mode of JNDI, so I decided to use the JNDI setting, so I need to solve the configuration of JNDI under Tomcat. I have found a lot of information. Speaking of this, I have to complain about our domestic blog forums. Most of them are copied, but the key is that they are still incomplete, full of mistakes and omissions, and do not indicate the original author.

Environment

Eclipse:Luna

Tomcat:apache-tomcat-9.0.8

Conceptual data sources and connection pooling

The data source name (data source name,DSN) is a data structure that contains information about a specific database, which is the necessary information that the open database connection driver can connect to the database. In fact, it essentially connects the program to the database. There is no real data in the data source, it just records which database you connect to and how you connect, such as odbc data sources. In other words, the data source is only the connection name of the database, and a database can have multiple data source connections.

In the Java language, a DataSource object is an object that represents a data source entity. A data source is a tool for storing data. It can be a complex, large enterprise database or a file that is as simple as rows and columns. The data source can be located on the server side or on the customer server side.

Connection pool: in the Java program, when we need to operate on the database, the premise of the addition, deletion, modification and query of the database is to establish a connection with the database, and the connection management is actually based on the data source, that is, connection pool.

Common database connection pooling:

The serial number connection pool name depends on the datasource class of the jar package implementation Note 1JNDI the data source is initialized, created, and managed by the corresponding web server (for example: tomcat,weblogic,websphere). There is no need to introduce a special jar package into the program. Javax.sql.datasource

2C3P0c3p0-0.9.xxx.jarcom.mchange.v2.c3p0.ComboPooledDataSource

3DBCPcommons-dbcp.jar

Commons-pool.jarorg.apache.commons.dbcp.BasicDataSource

4BoneCPbonecp-0.6.5.jar

Google-collections-1.0.jar

Slf4j-api-1.5.11.jar,slf4j-log4j12-1.5.11.jar

Log4j-1.2.15.jarBoneCPDataSource

What is JNDI?

Jndi's full name is java naming and directory interface. To put it simply, you name something according to the naming convention, and then you can use that name to find it directly in a specific environment.

JNDI is an API used to provide directory and naming capabilities to Java programs. You can simply think of JNDI as a technique for binding objects to names, and the object factory is responsible for producing objects that are bound to unique names. An external program can obtain a reference to an object by name. In a file system, the file name is bound to the file. In DNS, an IP address is bound to a URL. In a directory service, an object name is bound to an object entity.

Directory service (Directory service) is very important in both Intranets (Intranet) and Internates (Internet). It standardizes naming rules and makes it easy for people to understand entities and their relationships. JNDI is a standard extension of the Java platform that provides a set of interfaces, classes, and concepts about namespaces. The technologies currently supported by JNDI include LDAP, CORBA Common Object Service (COS) name service, RMI, NDS, DNS, Windows registry, and so on.

Jndi is designed to be independent of specific directory services, so a variety of directories can be accessed in the same way. In this way, java programmers using jndi can not only get uniform and regular naming and directories, but also seamlessly access (seamless acess) directory objects through a multi-tier naming scheme.

Advantages of JNDI

JNDI is a web server that implements java.sql.datasource. The web server is responsible for initializing the data source, creating the connection, assigning and managing the connection. Because the functionality itself is implemented by the web server, you do not need to introduce a special jar package into the project project, but you need to add the relevant configuration in some configuration files of the server. It has a good effect for us to connect to multiple databases.

Configuration of JDNI in Tomcat Jar package

1. Driver package for the database:

Jconn4.jar Sybase driver package

Mssql-jdbc-6.1.0.jre7.jar SQL Server driver package

2.JSP tag Ja package

. jstl-1.2.jar

Standard-1.1.2.jar

Test preparation

Create a new test Web project as follows:

JSP:

JNDI data source test

Tomcat:

Put the database-driven Jar package under the lib folder of Tomcat:

JNDI configuration

Defined within the GlobalNamingResources tag in the Tomcat configuration file server.xml (apache-tomcat-9.0.8\ conf)

Global reference

It is referenced in the context.xml (apache-tomcat-9.0.8\ conf) of Tomcat and is used for all projects under this Tomcat server.

WEB-INF/web.xml WEB-INF/tomcat-web.xml ${catalina.base} / conf/web.xml

Of course, we can also write the data source directly in the Context tag without referring to the locally configured:

WEB-INF/web.xml WEB-INF/tomcat-web.xml ${catalina.base} / conf/web.xml local reference

Local reference: that is, the configured data source is used only for the specified project.

Method 1: configure within the host tag in server.xml

Of course, it can also be defined directly under the Context tag

Method 2: create a new xml file under apache-tomcat-9.0.8\ conf\ Catalina\ localhost under the Tomcat installation directory with the same file name as the project name.

Method 3:

Create a new Context.xml configuration JNDI reference in the META-INF of the project directory under Tomcat, such as:, apache-tomcat-9.0.8\ webapps\ jndi_demo\ META-INF,.

Or

The advantage of this is that you can get rid of the Tomcat configuration changes.

Be careful

1. Read a lot of articles, some are in the project web.xml configuration of the following content, it has been verified that Tomcat is optional, but some articles say it is best to add, in order to facilitate the project migration, because some servers are required.

Index.jsp Sybase DB Connection jdbc/Sybase_claims javax.sql.DataSource Container SQLServer DB Connection jdbc/sqlserver javax.sql.DataSource Container

JNDI configuration we can find that the third way is completely separated from the configuration of Tomcat and has less impact on server. However, I recommend global configuration and references everywhere to facilitate switching.

After the configuration of Tomcat is modified, it is best to restart Tomcat to take effect. The modification of server.xml must be restarted, and tomcat will be loaded automatically without restart after the context.xml configuration is saved.

There are two ways to get JNDI data sources, the former is for Tomcat, while the latter is for IBM WebSphere.

A: java:comp/env/jdbc/Sybase_claims

B: jdbc/Sybase_claims

For A:

Java:comp/env is the environment naming context (environment naming context (ENC)), which was introduced after the EJB specification 1.1. it is introduced to solve the conflict caused by the original JNDI lookup, and to improve the portability of EJB or J2EE applications.

Common references in J2EE are:

JDBC data source references are declared in the java:comp/env/jdbc subcontext

The JMS connection factory is declared in the java:comp/env/jms subcontext

The JavaMail connection factory is declared in the java:comp/env/mail subcontext

The URL connection factory is declared in the java:comp/env/ URL subcontext

The above content is what is the implementation of Java using JNDI to connect to the database? have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report