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
(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.
Here is the test code:
- package com.zww.server;
- import java.beans.PropertyVetoException;
- import java.sql.Connection;
- import java.sql.SQLException;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- public final class ConnectionManager {
- //Creating a database connection pool using the single-profit model
- private static ConnectionManager instance;
- private static ComboPooledDataSource dataSource;
- private ConnectionManager() throws SQLException, PropertyVetoException {
- dataSource = new ComboPooledDataSource();
- dataSource.setUser("root"); //User name
- dataSource.setPassword("123456"); //Password
- dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/zww");//Database Address
- dataSource.setDriverClass("com.mysql.jdbc.Driver");
- dataSource.setInitialPoolSize(5); //Number of Initial Connections
- dataSource.setMinPoolSize(1);//Minimum number of connections
- dataSource.setMaxPoolSize(10);//maximum connection
- dataSource.setMaxStatements(50);//Maximum waiting time
- dataSource.setMaxIdleTime(60);//Maximum idle time, in milliseconds
- }
- public static final ConnectionManager getInstance() {
- if (instance == null) {
- try {
- instance = new ConnectionManager();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return instance;
- }
- public synchronized final Connection getConnection() {
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- }
- package com.zww.server;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
- public class ConnectionDemo {
- public static void main(String[] args) throws SQLException {
- System.out.println("Use connection pools................................");
- for (int i = 0; i < 20; i++) {
- long beginTime = System.currentTimeMillis();
- Connection conn = ConnectionManager.getInstance().getConnection();
- try {
- PreparedStatement pstmt = conn.prepareStatement("select * from event");
- ResultSet rs = pstmt.executeQuery();
- while (rs.next()) {
- // do nothing...
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- long endTime = System.currentTimeMillis();
- System.out.println("The first" + (i + 1) + "The second execution takes time to:" + (endTime - beginTime));
- }
- System.out.println("No connection pooling................................");
- for (int i = 0; i < 20; i++) {
- long beginTime = System.currentTimeMillis();
- MysqlDataSource mds = new MysqlDataSource();
- mds.setURL("jdbc:mysql://localhost:3306/zww");
- mds.setUser("root");
- mds.setPassword("123456");
- Connection conn = mds.getConnection();
- try {
- PreparedStatement pstmt = conn.prepareStatement("select * from event");
- ResultSet rs = pstmt.executeQuery();
- while (rs.next()) {
- // do nothing...
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- long endTime = System.currentTimeMillis();
- System.out.println("The first" + (i + 1) + "The second execution takes time to:"
- + (endTime - beginTime));
- }
- }
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