Configure how the database connection pool uses JNDI

1. JNDI:

JNDI is ( Java Naming and Directory Inteface)Java Name directory interface.

The role of JNDI is to bring resources into the server.You can think of JNDI as a repository.Put Java objects into JNDI.

2. Origin of data sources:

In java development, use JDBC operations data base Several steps:

1. Use Class.forName (full path name of the class): to load the database driver.

2. Get the Connection connection object for the database.DriverManager.getConnection().

3. Operating databases: querying databases or updating database contents,

4. Close the database connection: use the close method.

Note: There are four steps to get one database connection at a time, but among them, [1], [2], [4] are common operations for all databases, and only [3] are different steps for operating databases.It takes time to get the connection object of the database and to close the connection of the database.Causes poor performance.

If we have already created many connection objects in the beginning and put them in a public place, when there is a request to connect to the database, we will pull a connection from this public place, operate the database, complete the database operation, not close the connection, but put it in the public warehouse, which will bring up the thing of the database connection pool, that is, store moreConnection object.

3. Configure the connection pool of a database using JNDI in two ways: (global JNDI configuration and non-global JNDI configuration)

(1) Non-global JNDI configuration: he only configures data sources for a Web project

1. Import the jar package file to link the database.

For example, sqlserver import: sqljdbc4.jar package

Oracle Import: ojdbc14.jar package

MySQL Import: mysql -connector-java-5.0.8.jar package

2. Configure the connection pool for the database in JNDI:

Create a context.xml file in META-INF in the WEB project.Connection pool information for setting up a database

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Context>  
  3. <!--       among Resource The tag is the configuration resource whose attribute values are as follows:  
  4.   |- name: Indicates the name to look for later.You can find it by this name DataSource,This name can be changed arbitrarily, but it is the name that the program will ultimately look for.  
  5.            To avoid confusion with other names, use jdbc/oracle,The configuration is now one jdbc About oracle Naming service for.  
  6.   |- auth: Authorized and managed by the container, refers to whether the username and password are valid on the container and can be used Container  
  7.   |- type: The type represented by this name is now javax.sql.DataSource(No change)  
  8.   |- maxActive: Represents the maximum number of connections a database can open on this server  
  9.   |- maxIdle: Represents the minimum number of connections a database maintains on this server  
  10.   |- maxWait: Maximum wait time.10,000 milliseconds  
  11.   |- username: User name for database connection  
  12.   |- password: Password for database connection  
  13.   |- driverClassName: Driver for Database Connection  
  14.   |- url: Address of database connection  
  15. -->  
  16. <!--To configure Oracle Database JNDI data source-->  
  17. <Resource   
  18.         name="jdbc/oracle"  
  19.         auth="Container"   
  20.         type="javax.sql.DataSource"  
  21.         maxActive="100"   
  22.         maxIdle="30"   
  23.         maxWait="10000"  
  24.         username="lead_oams"   
  25.         password="p"  
  26.         driverClassName="oracle.jdbc.driver.OracleDriver"  
  27.         url="jdbc:oracle:thin:@192.168.1.229:1521:lead"/>  
  28.   
  29. <!--To configure MySQL Database JNDI data source-->  
  30. <Resource   
  31.         name="jdbc/mysql"  
  32.         auth="Container"   
  33.         type="javax.sql.DataSource"  
  34.         maxActive="100"   
  35.         maxIdle="30"   
  36.         maxWait="10000"  
  37.         username="root"   
  38.         password="root"  
  39.         driverClassName="com.mysql.jdbc.Driver"  
  40.         url="jdbc:mysql://192.168.1.144:3306/leadtest?useUnicode=true&amp;characterEncoding=utf-8"/>  
  41.   
  42. <!--To configure SQLServer Database JNDI data source-->  
  43. <Resource   
  44.         name="jdbc/sqlserver"  
  45.         auth="Container"   
  46.         type="javax.sql.DataSource"  
  47.         maxActive="100"   
  48.         maxIdle="30"   
  49.         maxWait="10000"  
  50.         username="sa"   
  51.         password="123456"  
  52.         driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"  
  53.         url="jdbc:sqlserver://192.168.1.51:1433;DatabaseName=demo"/>  
  54. </Context>  
3. Configure in the web.xml file (optional or nonexistent): if any:
  1. <resource-ref>  
  2.     <description>my DB Connection</description>  
  3.     <res-ref-name><span style="font-family: Arial, Helvetica, sans-serif;">jdbc/sqlserver</span><span style="font-family: Arial, Helvetica, sans-serif;"></res-ref-name>This name should match the context.xml In name equally</span>  
  4.     <res-type>javax.sql.DataSource</res-type>  
  5.     <res-auth>Container</res-auth>  
  6. </resource-ref>  
4. If Spring is used: in the applicationContext.xml or in your own Spring configuration file: import the JNDI configuration information

//Get connection pool information for the JNDI-configured database in content.xml.jndi-name must be the same as the name value in JNDI

<jee:jndi-lookup id="dataSource"  jndi-name="jdbc/sqlserver" />

//Use JdbcTemplate to operate the database.

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

Use IOC to get a jdbcTemplate object in a java file.Finally, use JdbcTemplate to manipulate the database

5. If not used spring In a java file:

Context initContext = new InitialContext();

Context envContext = (Context)initContext.lookup("java:/comp/env"); //Fixed, no modification required
 DataSource ds = (DataSource)envContext.lookup(jdbc/sqlserver);

(2) Global JNDI:

1. Add under the GlobalNamingResources tag in the server.xml file under conf in the tomcat installation directory (a partial JNDI resource match in the context.xml file)

Set information).

2. Configuring in the context under META-INF in a Web project

//global s should have the same name as the server.xml name, and the name here indicates that the name is currently being used.

<ResourceLink name="jdbc/sqlserver" global="jdbc/sqlserver" type="javax.sql.DataSource"/>

3. The remaining JNDI is the same as the local JNDI, and 3, 4, 5 can be used directly, that is, the local 2 is replaced by the global [1], [2].

Keywords: Database JDBC xml Java

Added by fredriksk on Sat, 22 Jun 2019 20:18:12 +0300