Method steps for configuring database connection pools in Java

  • 2020-04-01 01:51:56
  • OfStack

First, what is a database connection pool
The ideas of the database connection pool technology is very simple, connect the database as a Vector object is stored in the object, and once after the construction of the database connection, different database access request can share these links, in this way, by reusing the database connection has been established, can overcome the above shortcomings, greatly save system resources and time.


In the actual application development, especially in the WEB application system, if the JSP, Servlet or EJB using JDBC direct access to the data in the database, every data access requests must go through establishing a database connection, open the database, access data, and close the database connection and other steps, and the connection and open the database is a consumer of resources and time-consuming work, if frequent the database operation, the performance of the system will inevitably fell sharply, even lead to system crash. Database connection pooling is the most common way to solve this problem, and is provided in many application servers (such as Weblogic,WebSphere, and JBoss) without the need for programming, but it is essential to understand the technique in depth.

The main operations of the database connection pool are as follows:
(1) establish database connection pool object (server startup).
(2) create the initial number of database connections (that is, the number of idle connections) according to the parameters specified in advance.
(3) for a database access request, get a connection directly from the connection pool. If there are no idle connections in the database connection pool object and the number of connections does not reach the maximum (that is, the maximum number of active connections), create a new database connection.
(4) access to database.
(5) close the database and release all database connections (at this point, close the database connection, not really close, but put it into the free queue. If the actual number of idle connections is greater than the original number of idle connections, the connection is released.
(6) release database connection pool objects (release database connection pool objects and release all connections during server shutdown and maintenance).
Configuration steps:
1. In Tomcat  In the conf/content. XML < Context> Add the following to the node

< Resource name=" JDBC /news" auth="Container"type=" javax.sql.datasource "maxActive="100" maxIdle="30" maxWait=" 10,000" username="sa" password="120010" DriverClassName = "com. Microsoft. Essentially. JDBC. SQLServerDriver" url = "JDBC: essentially: / / localhost: 1433; DatabaseName = news "/ >


Name: and then we're going to call resource
Type: is the class under the Java extension package used to link to the database
MaxActive: maximum number of connections
MaxIdle: is the maximum number of connections in idle time
Maxwait: idle judgment criterion
Note:
1. Chinese is not allowed in the configuration file; Properties are separated by a space
2. There is no need to store the jars corresponding to the linked database in the tomcat /lib folder in the project
The following is also linked to oracle

< The Resource type = "javax.mail. SQL. The DataSource name =" url "JDBC/news" = "JDBC: oracle: thin: @ 192.168.2.102:1521: wouldn" driverClassName = "... The oracle JDBC driver OracleDriver "password =" bg "username =" test "maxWait =" 10000" MaxIdle = "30" maxActive = "100" auth = "Container" / >
2. Add the following code to the web.xml of the project
< The resource - ref>
          < Res - ref - name> The JDBC/news< / res - ref - name>
          < Res - type> Javax.mail. SQL. DataSource< / res - type>
          < Res - auth> Container< / res - auth>
< / resource - ref>

Here < Res - ref - name> The name inside the tag is the name of the name in the content.xml file


3. Get the Connection object in the database Connection pool


The Context Context = new InitialContext ();
DataSourceds = (DataSource) context. The lookup (" Java: comp/env/JDBC/news ");
Connectionconn = ds. GetConnection ();


Context is a class under javax.namingx
DataSource is a class under javax.sql
In "Java :comp/env/ JDBC /news" : Java :comp/env is fixed JDBC /news is jndi name (the name configured in Tomcat)


If we are not configuring the data source in web.xml, we use spring to control the database link method steps:
1. Add the following node to the configuration file
< The dataSource bean id = "" class =" org. Springframework. Jndi. JndiObjectFactoryBean ">
      < The property name = "jndiName" value = "Java: comp/env/JDBC/news" / >  
< / bean>

How many pieces of SQL query data are configured:


      When invoked, the Dao class directly inherits the JdbcDaoSupport class (the class in spring.jar)
      Int count = this.getjdbctemplate (). QueryForInt ("select * from users");


So we're done! Of course, there may be many ways to configure, I only listed a few of the above, if there is any question can be private I am happy to discuss with you!
- ydcun


Related articles: