java database connection pool configuration

Keywords: Java MySQL JDBC SQL

jar package required for connection pool configuration

Servlet api.jar imports the jar package under Tomcat directory by itself. If there are no other jar packages

See download link ----- > download (there's no way to set it as free, set it as the lowest point to download)

Download portal 2: YouDaoNote

These jar packages can be configured with dbcp, c3p0 and other mainstream connection pool configurations

DBCP connection pool configuration

The configuration file dp.properties is recommended under the src directory of the house

The contents of the configuration file are as follows

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/databasename
db.username=username
db.password=password
dataSource.initialSize=10
dataSource.maxIdle=60
dataSource.minIdle=10
dataSource.maxActive=80
dataSource.maxWait=3000
removeAbandonedOnMaintenance=true
removeAbandonedOnBorrow=true
removeAbandonedTimeout=30

Modify the corresponding parameters according to your own database configuration.

Connection pool connection acquisition class

Build the class to get the connection as follows

package xxx.sqlcon;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp2.BasicDataSource;
public class ConnectionPool {
	private static BasicDataSource basicDataSource;

    /**
    * Read the configuration file and initialize the connection pool
    */
    private static void init(){
        // Read the configuration file according to the classpath above
        java.io.InputStream inputStream = ConnectionPool.class.getClassLoader()
                .getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            // Load properties file
            properties.load(inputStream);

            // Create a new BasicDataSource
            basicDataSource = new BasicDataSource();

            // Set corresponding parameters
            basicDataSource.setUrl(properties.getProperty("db.url"));
            basicDataSource.setDriverClassName(properties.getProperty("db.driverClassName"));
            basicDataSource.setUsername(properties.getProperty("db.username"));
            basicDataSource.setPassword(properties.getProperty("db.password"));

            basicDataSource.setInitialSize(Integer.parseInt(properties.getProperty("dataSource.initialSize")));
            basicDataSource.setMaxIdle(Integer.parseInt(properties.getProperty("dataSource.maxIdle")));
            basicDataSource.setMinIdle(Integer.parseInt(properties.getProperty("dataSource.minIdle")));
            basicDataSource.setMaxWaitMillis(Integer.parseInt(properties.getProperty("dataSource.maxWait")));
            basicDataSource.setMaxTotal(Integer.parseInt(properties.getProperty("dataSource.maxActive")));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Get Connection
     * @return Connection
     */
     public synchronized static Connection getConnection(){
            if (basicDataSource == null){
                init();
            }
            try {
                return basicDataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
      }
}

Call the getConnection() function to get an available connection.

 

Posted by coldwerturkey on Wed, 01 Jan 2020 18:04:42 -0800