Database Connection Pool (Design Patterns) - Database

Keywords: Database Java SQL JDBC

First, what is a database connection pool?

Official: Connection pooling is to set up enough database connections when the program starts, and make these connections into a connection pool. The program dynamically applies for, uses and releases the connections in the pool.
Personal Understanding: Creating database connections is a time-consuming operation, but also easy to create security risks to the database. Therefore, when the program is initialized, centralized creation of multiple database connections, and centralized management of them for the use of the program, can ensure faster database reading and writing speed, but also more secure and reliable.

II. Operation mechanism of database connection pool

(1) Creating connection pools at program initialization
(2) Apply to the connection pool for available connections when using
(3) Return the connection to the connection pool after use
(4) When the program exits, disconnect all connections and release resources

3. Use of database connection pool

As an open source database connection pool, C3P0 is an excellent connection pool with reliable performance.



First, go to http://sourceforge.net/projects/c3p0/, and download the corresponding jar packages, totaling three, as shown in the figure below.


Next, you import the jar package into the project, and then you can use cp30.
The sample code is as follows:
  1. package com.zww.server;  
  2.   
  3. import java.beans.PropertyVetoException;  
  4. import java.sql.Connection;  
  5. import java.sql.SQLException;  
  6. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  7.   
  8. public final class ConnectionManager {  
  9.     //Creating a database connection pool using the single-profit model  
  10.     private static ConnectionManager instance;  
  11.     private static ComboPooledDataSource dataSource;  
  12.   
  13.     private ConnectionManager() throws SQLException, PropertyVetoException {  
  14.         dataSource = new ComboPooledDataSource();  
  15.   
  16.         dataSource.setUser("root");     //User name  
  17.         dataSource.setPassword("123456"); //Password  
  18.         dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/zww");//Database Address
  19.         dataSource.setDriverClass("com.mysql.jdbc.Driver");  
  20.         dataSource.setInitialPoolSize(5); //Number of Initial Connections  
  21.         dataSource.setMinPoolSize(1);//Minimum number of connections  
  22.         dataSource.setMaxPoolSize(10);//maximum connection  
  23.         dataSource.setMaxStatements(50);//Maximum waiting time  
  24.         dataSource.setMaxIdleTime(60);//Maximum idle time, in milliseconds  
  25.     }  
  26.   
  27.     public static final ConnectionManager getInstance() {  
  28.         if (instance == null) {  
  29.             try {  
  30.                 instance = new ConnectionManager();  
  31.             } catch (Exception e) {  
  32.                 e.printStackTrace();  
  33.             }  
  34.         }  
  35.         return instance;  
  36.     }  
  37.   
  38.     public synchronized final Connection getConnection() {  
  39.         Connection conn = null;  
  40.         try {  
  41.             conn = dataSource.getConnection();  
  42.         } catch (SQLException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.         return conn;  
  46.     }  
  47. }  
Here is the test code:
  1. package com.zww.server;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7.   
  8. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;  
  9.   
  10.   
  11. public class ConnectionDemo {  
  12.   
  13.     public static void main(String[] args) throws SQLException {  
  14.         System.out.println("Use connection pools................................");  
  15.         for (int i = 0; i < 20; i++) {  
  16.             long beginTime = System.currentTimeMillis();  
  17.             Connection conn = ConnectionManager.getInstance().getConnection();  
  18.             try {  
  19.                 PreparedStatement pstmt = conn.prepareStatement("select * from event");  
  20.                 ResultSet rs = pstmt.executeQuery();  
  21.                 while (rs.next()) {  
  22.                      // do nothing...  
  23.                 }  
  24.             } catch (SQLException e) {  
  25.                 e.printStackTrace();  
  26.             } finally {  
  27.                 try {  
  28.                     conn.close();  
  29.                 } catch (SQLException e) {  
  30.                     e.printStackTrace();  
  31.                 }  
  32.             }  
  33.   
  34.             long endTime = System.currentTimeMillis();  
  35.             System.out.println("The first" + (i + 1) + "The second execution takes time to:" + (endTime - beginTime));  
  36.         }  
  37.   
  38.         System.out.println("No connection pooling................................");  
  39.         for (int i = 0; i < 20; i++) {  
  40.             long beginTime = System.currentTimeMillis();  
  41.             MysqlDataSource mds = new MysqlDataSource();  
  42.             mds.setURL("jdbc:mysql://localhost:3306/zww");  
  43.             mds.setUser("root");  
  44.             mds.setPassword("123456");  
  45.             Connection conn = mds.getConnection();  
  46.             try {  
  47.                 PreparedStatement pstmt = conn.prepareStatement("select * from event");  
  48.                 ResultSet rs = pstmt.executeQuery();  
  49.                 while (rs.next()) {  
  50.                                     // do nothing...  
  51.                 }  
  52.             } catch (SQLException e) {  
  53.                 e.printStackTrace();  
  54.             } finally {  
  55.                 try {  
  56.                     conn.close();  
  57.                 } catch (SQLException e) {  
  58.                     e.printStackTrace();  
  59.                 }  
  60.             }  
  61.             long endTime = System.currentTimeMillis();  
  62.             System.out.println("The first" + (i + 1) + "The second execution takes time to:"  
  63.                                 + (endTime - beginTime));  
  64.         }  
  65.   
  66.     }  

The results of the operation are as follows:

The test results show that when using connection pool, only the first initialization is time-consuming. After initialization, using connection pool for database operation is obviously less time-consuming than not using connection pool.


This article changes from https://blog.csdn.net/qzc70919700/article/details/79984604

Posted by cswells on Sat, 11 May 2019 23:05:28 -0700