首页 > 代码库 > c3p0数据库连接池

c3p0数据库连接池

一、封装数据库连接(与dbcp类似)

1、下载

https://sourceforge.net/projects/c3p0/?source=typ_redirect

2、导入jar

mchange-commons-java-0.2.11.jar c3p0-0.9.5.2.jar

3、编写配置文件c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <!-- This app is massive! -->
    <named-config name="c3p0">
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/hibernate
        </property>

        <!-- 若数据库连接不足时一次性向数据库申请多少连接 -->
        <property name="acquireIncrement">5</property>
        <property name="initialPoolSize">5</property>
        <property name="minPoolSize">5</property>
        <property name="maxPoolSize">10</property>
    </named-config>
</c3p0-config>

4、编写测试

@Test
    public void testC3p0() {
        DataSource ds = new ComboPooledDataSource("c3p0");
        try {
            System.out.println(ds.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

 

c3p0数据库连接池