The principle and Simulation of mysql database connection pool

Keywords: Java Database JDBC MySQL

What is a database connection pool?

When the system uses JDBC technology to access the database, it will create a connection object, and the creation process of the object is very resource consuming, and the time to create the object is also very long. Assuming that the system has 10000 accesses a day, then 10000 connection objects will be created a day, which greatly wastes the database resources, and may cause the database server Memory overflow, down.

In order to solve the above problems, database connection pool is introduced, which is mainly used to allocate, manage and release database connections. When the system starts, the database connection pool will first create several (configurable) connection objects and put them into the pool. When the system needs the connection object,
The database connection pool will allocate a pre created connection object from the pool to the system. When the system is used up or timeout, the database connection pool will put the connection object back into the pool.
This reduces the resources and time required to create connection objects and improves the performance of database operations.

The following figure is the schematic diagram of database connection pool:

 

 

 


Simulate writing a database connection pool

To use the database, you need to copy the JDBC related code and jar package written before and create a SimpleConnectionPool class, which needs to implement the following three functions:

1. Initialize a database connection pool and add 10 database connections to it;
2. Get the connection from the connection pool;
3. When the program runs out of connections, it needs to put the connection back into the connection pool.

Need to note: database connection pool to ensure thread safety!

 

/**
 * 1. Initialize a database connection pool and add 10 database connections to it;
 * 2. Get the connection from the connection pool;
 * 3. When the program runs out of connections, it needs to put the connection back into the connection pool.
 */
public class SimpleConnectionPool {
    
    //Create a pool to hold the connection. Pay attention to thread safety
    //Because the database connection pool needs to be taken out and stored frequently, the LinkedList Pond
    public static LinkedList<Connection> pool = (LinkedList<Connection>) Collections.synchronizedList(new LinkedList<Connection>());
    
    //Store 10 database connections in the database connection pool after class loading
    static {

        try {
            for (int i = 0; i < 10; i++) {
                Connection con = DBUtil.getConnection();
                pool.add(con);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    
    //Get connection from connection pool
    public static Connection getConnectionFromPool(){
        Connection con = null;
        //Determine whether there are connected objects in the pool
        if(pool.size()>0){
            con = pool.removeFirst();
        }else{
            //There are no available connections in the database connection pool
            throw new RuntimeException("The server is busy. Please try again later");
        }
        return con;
    }
    
    //When the program runs out of connections, it needs to put the connection back into the connection pool
    public static void release(Connection con){
        pool.addLast(con);
    }

}
public class DBUtil {
    
    private static String driverClass="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3306/map?useUnicode=true&amp;characterEncoding=utf-8";
    private static String userName="root";
    private static String password="root";
    
    static{
        try {
            //Load driver
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection(url, userName, password);
    }

}

 

 

Welcome to WeChat public number [Java classics], and watch more Java technology dry goods!

Scan wechat as below ↓ QR code attention

 

Posted by Dowdy on Thu, 28 Nov 2019 04:52:55 -0800