Maven project cityFileFK03 database connection pool connection (DBCP)

Database connection pool

1. Comparison of database connection pool and JDBC

The connection pool is the technology of the database.
JDBC creates a new connection object for each user request , and cancels the user after use. When multiple users access at the same time, the connection object is frequently created and destroyed, and the memory destruction is large;
while the connection pool is initialized, a batch is initialized. The database connection object is returned to the connection pool when it is used up to realize the reuse of connection objects. It is not enough to create a new batch of connection objects. When the maximum number of connections is reached, wait for the request to be released and returned to the connection pool

2. The use of database connection pool

2.1 Guide package

Go to the maven website to search for the required connection pool, copy the code, and go to the pom.xml file of the project to import the dependencies.

Insert picture description here

2.2 Use

2.2.1 Configure db.properties

Increase initial capacity and maximum capacity under db.properties

initSize = p.getProperty("db.initSize");
maxSize = p.getProperty("db.maxSize");

db.properties in the resources directory

db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/citylifefk?characterEncoding=utf8&serverTimezone=UTC
db.driver=com.mysql.jdbc.Driver
db.initSize = 25
db.maxSize = 50

2.2.2 DBUtil class in the util directory

Define 6 static variables, load data once to share data

    private static  String username;
    private static  String password;
    private static  String url;
    private static  String driver;
    private static String initSize;
    private static String maxSize

Create a connection pool object

private static BasicDataSource basicDataSource;

Static block content:

			static {
    
    

        try {
    
    
            InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");

            //  FileInputStream in=new FileInputStream("db.properties");
            System.out.println("in:" + in);

            //Properties 将properties属性文件数据以k-v的形式加载到内存中
            Properties p = new Properties();
            //p.load(in)将int类型加载
            p.load(in);

            //properties里保存到变量中
            username = p.getProperty("db.username");
            password = p.getProperty("db.password");
            url = p.getProperty("db.url");
            driver = p.getProperty("db.driver");
            initSize = p.getProperty("db.initSize");
            maxSize = p.getProperty("db.maxSize");

            System.out.println(username);
            System.out.println(password);
            System.out.println(url);
            System.out.println(driver);

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

    }


Use the BasicDataSource method to encapsulate a method to obtain a connection pool

basicDataSource = new BasicDataSource();

Set the parameters of the database to the connection pool object and return to basicDataSource

    basicDataSource.setDriverClassName(driver);
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);
    basicDataSource.setUrl(url);
    basicDataSource.setInitialSize(Integer.parseInt(initSize));
    basicDataSource.setMaxActive(Integer.parseInt(maxSize));
    return  basicDataSource;

Get the connection object through the connection pool

	//通过连接池获取连接对象
    public static Connection getConn(){
    
    
        Connection connection = null;
        try {
    
    
            connection = DBUtil.getBData().getConnection();
        } catch (SQLException e) {
    
    
            e.printStackTrace();
        }
        return  connection;
    }

The complete code of DBUtil is in the util directory:

package util;

/*

连接数据的类

 */


import org.apache.commons.dbcp.BasicDataSource;

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

public class DBUtil {
    
    
    private static String username;
    private static String password;
    private static String url;
    private static String driver;
    private static String initSize;
    private static String maxSize;
    private static BasicDataSource basicDataSource;


    static {
    
    

        try {
    
    
            InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");

            //  FileInputStream in=new FileInputStream("db.properties");
            System.out.println("in:" + in);
            Properties p = new Properties();
            p.load(in);

            username = p.getProperty("db.username");
            password = p.getProperty("db.password");
            url = p.getProperty("db.url");
            driver = p.getProperty("db.driver");
            initSize = p.getProperty("db.initSize");
            maxSize = p.getProperty("db.maxSize");

            System.out.println(username);
            System.out.println(password);
            System.out.println(url);
            System.out.println(driver);

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

    }

    //封装一个获取连接池的连接方法
    public static BasicDataSource getBData() {
    
    
    basicDataSource = new BasicDataSource();
    //将数据库的参数设置到连接池对象中
    basicDataSource.setDriverClassName(driver);
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);
    basicDataSource.setUrl(url);
    //将字符串转为Integeter类型
    basicDataSource.setInitialSize(Integer.parseInt(initSize));
    basicDataSource.setMaxActive(Integer.parseInt(maxSize));

    return basicDataSource;

    }

    //通过连接池获取连接对象
    public static Connection getConn(){
    
    
        Connection connection = null;
        try {
    
    
            connection = DBUtil.getBData().getConnection();
        } catch (SQLException throwables) {
    
    
            throwables.printStackTrace();
        }
        return connection;
    }
}

2.2.3 Test

Create a new test class under the Java package in the Test package: DBTest

import org.junit.Test;
import util.DBUtil;

import java.sql.Connection;

public class DBTest {
    
    

    //单元测试 junit 单元测试包
    /*单元测试方法
    注意:1 方法返回值只能写void
    2 方法不能传参数
    3 方法上面使用@Test注解*/
    @Test
    public void test1(){
    
    

        Connection conn = DBUtil.getConn();
        System.out.println("conn:"+conn);
    }
}

Test Results:
Insert picture description here

Printed the conn database connection pool connection is successful

Guess you like

Origin blog.csdn.net/qq_43881663/article/details/112875921