数据库 数据库连接池(DBCP和C3P0)

数据库连接池

池化技术:准备一些预先的资源,过来就连接预先准备好的

最小连接数:10个
最大连接数:20业务最高承载上限
等待超时:100ms

开源数据源

DBCP
C3P0


DBCP

需要的jar包 (commons-dbcp-1.4)(commons-pool-1.6)

DBCP配置文件
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=

#<!-- 初始化连接 -->
initialSize=10

#最大连接数量
maxActive=50

#<!-- 最大空闲连接 -->
maxIdle=20

#<!-- 最小空闲连接 -->
minIdle=5

#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=60000

#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=utf8

#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true

#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=

#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED
DBCP工具类
private static  DataSource dataSource = null;

    static {
        try {
            InputStream in         = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
            Properties  properties = new Properties();
            properties.load(in);

            // 创建数据源
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 获取连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }


    // 释放连接资源
    public static void release(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
        if (resultSet != null) {
            resultSet.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }

C3P0

需要的jar包 (c3p0-0.9.5.5)(mchange-commons-java-0.2.20)

c3p0配置文件
<c3p0-config>
    <!--
    C3P0的缺省(默认)配置,
    如果在代码中“”这样写就表示使用的是C3P0的缺省(默认)配置。
    -->
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcStudy?useSSL=false&amp;characterEncoding&amp;useUnicode=true</property>
        <property name="user">root</property>
        <property name="password">132654</property>

        <property name="acquireIncrement">5</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">5</property>
        <property name="maxPoolSize">20</property>
    </default-config>


    <!--
    C3P0的命名配置,
    如果在代码中“”这样写就表示使用的是name是mysql的配置。
    -->
    <named-config name="MySQL">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcStudy?useSSL=false&amp;characterEncoding&amp;useUnicode=true</property>
        <property name="user">root</property>
        <property name="password">132654</property>

        <property name="acquireIncrement">5</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">5</property>
        <property name="maxPoolSize">20</property>
    </named-config>

</c3p0-config>
c3p0工具类
    private static  ComboPooledDataSource dataSource = null;

    static {
        try {
            // 代码版配置
            // dataSource = new ComboPooledDataSource();
            // dataSource.setDriverClass();
            // dataSource.setUser();
            // dataSource.setPassword();
            // dataSource.setJdbcUrl();
            //
            // dataSource.setMaxPoolSize();
            // dataSource.setMinPoolSize();


            // 创建数据源
            // 配置文件写法
            dataSource = new ComboPooledDataSource("MySQL");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 获取连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }


    // 释放连接资源
    public static void release(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
        if (resultSet != null) {
            resultSet.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
posted @   鸣于乔木  阅读(84)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示