Fork me on GitHub

c3p0连接池配置以及使用

1.所需要的jar包

https://mvnrepository.com/artifact/com.mchange/c3p0

https://mvnrepository.com/artifact/com.mchange/mchange-commons-java/0.2.15

 

 

2.配置c3p0-config.xml文件 放在src文件夹下

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <!--  默认配置  -->
    <default-config>
        <!--   连接参数  -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/Manager</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <!--  连接池参数  -->
        <!-- 初始化申请的连接数量 -->
        <property name="initialPoolSize">5</property>
        <!-- 最大的连接数量 -->
        <property name="maxPoolSize">10</property>
        <!-- 超时时间 -->
        <property name="checkoutTimeout">3000</property>
    </default-config>
    <!--c3p0配置1-->
    <named-config name="c3p0">
        <!--   连接参数  -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/manager</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <!--  连接池参数  -->
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">8</property>
        <property name="checkoutTimeout">1000</property>
    </named-config>
</c3p0-config>

 

3.配置c3p0数据源工具类

public class ComboPooledDataSourceUtil {
private static ComboPooledDataSource dataSource =new ComboPooledDataSource("c3p0");
//xml编码 public static DataSource getDataSourceByxml(){
return dataSource;
} }

 

4.测试

  @Test
    //测试c3p0
    public void test_c3p0() throws PropertyVetoException, SQLException {
        Connection connection = ComboPooledDataSourceUtil.getDataSourceByxml().getConnection();
        //Connection connection = ComboPooledDataSourceUtil.getDataSource().getConnection();
        System.out.println(connection);
    }

 

posted @ 2020-06-17 11:16  亲爸爸  阅读(1226)  评论(0编辑  收藏  举报