DBCP database connection pool

Keywords: Database Java JDBC MySQL

DBCP database connection pool

DBCP(DataBase Connection Pool)

When developing a project, we need to constantly interact with the database. In java, using JDBC to connect to the database every time we connect to the database, we need to load the driver, get the connection, destroy the connection and other steps, which takes a lot of time. In order to solve this problem, database connection pool technology appears.
DBCP is a kind of java database connection pool developed by Apache. When the program starts, some database connection objects are created first. As long as the database connection is needed, an object is obtained from the object pool and returned to the object pool after use. It allows programs to automatically manage the release and disconnection of database connections.

Required jar package:

commons-dbcp2-2.4.0,commons-logging-1.2,commons-pool2-2.6.1,mysql-connector-java-5.1.37-bin
Links: https://pan.baidu.com/s/120tXYYDtpAVveWOfKA8T8g
Extraction code: x5hi

Steps:

  1. Import the above jar package into the project
  2. Create a new dbcp.properties configuration file
########DBCP configuration file##########
#Driver name
driverClassName=com.mysql.jdbc.Driver
#url
url=jdbc:mysql://127.0.0.1:3306 / database name
#User name
username=root
#Password
password=root
#Number of initial connections
initialSize=3
#Maximum active number
maxTotal=15
#Maximum idle number
maxIdle=15
#Minimum idle number
minIdle=3
#Maximum wait time (MS)
maxWaitMillis=5000
#Whether the connection in the program is recycled by the connection pool after it is not used (this version uses removeAbandonedOnMaintenance and removeAbandonedOnBorrow)
#removeAbandoned=true
removeAbandonedOnMaintenance=true
removeAbandonedOnBorrow=true
#The connection will be deleted (in seconds) if it is not used within the specified number of seconds (configured as 1 second for matching the test program)
removeAbandonedTimeout=5
  1. Create a new DBCPUtil.java tool class. Some codes are as follows
public class DBCPUtil {
	private static Properties prop = new Properties();
	private static DataSource dataSource;
	
	static{
		FileInputStream fis = null;
		try {
			//Read dbcp.properties into
			fis = new FileInputStream("config/dbcp.properties");
			//Using the Properties collection to load the read configuration file into memory
			prop.load(fis);
			//Create dataSource
			dataSource = BasicDataSourceFactory.createDataSource(prop);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(fis!=null){
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static Connection getConnection(){
		Connection conn = null;
		try {
			//Get database connection
			dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
}
  1. Complete the linked database tool class and use JUnit 4 for unit test
//Use JUnit 4: right click project name - > bulid path - > Add Libraries - > JUnit
	@Test
	public void test(){
		System.out.println(getConnection());
	}
  1. The following results show that the connection was successful

Posted by Stagnate on Tue, 03 Dec 2019 10:48:25 -0800