DBCP数据库连接池的两种实现方式

来自姬鸿昌的知识库
跳到导航 跳到搜索

https://www.bilibili.com/video/BV1eJ411c7rf?p=49

pom.xml 引入依赖

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.5.5</version>
        </dependency>




Java 代码使用 DBCP 数据库连接池

package io.github.jihch.connection;

import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class DBCPTest {

    /**
     * 测试DBCP的数据库连接池技术
     */
    @Test
    public void testGetConnection() throws SQLException {
        //创建了 DBCP 的数据库连接池
        BasicDataSource source = new BasicDataSource();

        //设置基本信息
        source.setDriverClassName("com.mysql.cj.jdbc.Driver");
        source.setUrl("jdbc:mysql://localhost:3306/jdbc_test?rewriteBatchedStatements=true");
        source.setUsername("root");
        source.setPassword("123456");

        //还可以设置其他涉及数据库连接池管理的相关属性
        source.setInitialSize(10);
        source.setMaxActive(10);
        //……

        Connection conn = source.getConnection();
        System.out.println(conn);

    }

}



DBCP 连接池常用基本配置属性

initialSize

连接池启动时创建的初始化连接数量(默认值为0)


maxActive

连接池中可同时连接的最大的连接数(默认值为8,调整为20,高峰单机器在20并发左右,自己根据应用调整)


maxIdle

连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负数表示不限制(默认为8个,maxIdle不能设置太小,因为假如在高负载的情况下,连接的打开时间比关闭的时间快,会引起连接池中 idle 的个数上升超过maxIdle,而造成频繁的连接销毁和创建,类似于jvm参数中的Xmx设置)


maxWait

最大等待时间,当没有可用连接时,连接池等待连接释放的最大时间,超过该时间限制会抛出异常,如果设置-1表示无限等待(默认为无限,调整为60000ms,避免因线程池不够用,而导致请求被无限制挂起)


poolPreparedStatements

开启池的prepared(默认是false,未调整,经过测试,开启后的性能没有关闭的好。)


maxOpenPreparedStatements

开启池的prepared 后的同时最大连接数(默认无限制,同上,未配置)


minEvictableIdleTimeMillis

连接池中连接,在时间段内一直空闲, 被逐出连接池的时间


removeAbandonedTimeout

超过时间限制,回收没有用(废弃)的连接(默认为 300 秒,调整为180)


removeAbandoned

超过 removeAbandonedTimeout 时间后,是否进行没用连接(废弃)的回收(默认为false,调整为true)

  1. 如果开启了removeAbandoned,当getNumIdle() < 2) and (getNumActive() > getMaxActive() - 3)时被触发.
  2. 举例:当maxActive=20,活动连接为18,空闲连接为1时,可以触发“removeAbandoned”。但是活动连接只有在没有被使用的时间超过“removeAbandonedTimeout”时才被回收


logAbandoned

标记当连接被回收时是否打印程序的stack traces日志(默认为false,未调整)



推荐使用外部配置文件配置 DBCP 连接池

dbcp.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc_test?rewriteBatchedStatements=true
username=root
password=123456


代码

    //推荐使用的方式二:使用配置文件,
    @Test
    public void testGetConnection1() throws Exception {
        Properties pros = new Properties();

        //方式1:
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");

        //方式2:
//        File file = new File("src/main/resources/dbcp.properties");
//        FileInputStream is = new FileInputStream(file);

        pros.load(is);

        DataSource dataSource = BasicDataSourceFactory.createDataSource(pros);
        Connection conn = dataSource.getConnection();
        System.out.println(conn);

    }



https://github.com/jihch/jdbc_2/blob/main/src/main/resources/dbcp.properties

https://github.com/jihch/jdbc_2/blob/main/src/main/java/io/github/jihch/connection/DBCPTest.java

https://github.com/jihch/jdbc_2/blob/main/src/main/java/io/github/jihch/betterutil/JDBCUtils.java

https://github.com/jihch/jdbc_2/blob/main/src/test/java/io/github/jihch/betterutil/JDBCUtilsTest.java