1. 需要导入的包
- c3p0-0.9.5.2.jar
- mchange-commons-0.2.15.jar
- mysql-connector.jar
2. 配置xml
创建c3p0-config.xml文件,名字不能改动,并且放到src下,c3p0包会自动到src下查找c3p0-config.xml,名字错了,地方不对都不能配置成功
配置
<c3p0-config>
<!-- 默认配置,如果没有指定使用则使用这个配置 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password"></property>
<property name="checkoutTimeout">30000</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">40</property>
<property name="minPoolSize">30</property>
<property name="maxPoolSize">50</property>
<property name="maxStatements">200</property>
<property name="maxStatementsPerConnection">20</property>
</default-config>
<!-- 自定义配置 -->
<named-config name="Howl">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/zero_demo</property>
<property name="user">root</property>
<property name="password"></property>
<property name="checkoutTimeout">30000</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">40</property>
<property name="minPoolSize">30</property>
<property name="maxPoolSize">50</property>
<property name="maxStatements">200</property>
<property name="maxStatementsPerConnection">20</property>
</named-config>
</c3p0-config>
或者到官网查看标准配置 C3P0
3. C3P0Util工具类
public class C3P0Util {
// num = 1 使用默认的
private static ComboPooledDataSource comboPooledDataSourceDefault = new ComboPooledDataSource();
// num = 2 使用自定义的
private static ComboPooledDataSource comboPooledDataSourceMydefine = new ComboPooledDataSource("Howl");
public static Connection getConnection(int num){
try {
if(num == 1){
return comboPooledDataSourceDefault.getConnection();
}else if(num == 2){
return comboPooledDataSourceMydefine.getConnection();
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
4. 测试
public class Test {
public static void main(String[] args) throws SQLException {
// 分别在两个不同的库设计了一张表,数据不同
String sql = "SELECT * FROM user";
// 使用默认数据源
Connection connOne = C3P0Util.getConnection(1);
PreparedStatement psOne = connOne.prepareStatement(sql);
ResultSet rsOne = psOne.executeQuery();
while(rsOne.next()){
System.out.println(rsOne.getString("username"));
}
connOne.close();
System.out.println("-------人工分割线-------");
// 使用自定义的数据源
Connection connTwo = C3P0Util.getConnection(2);
PreparedStatement psTwo = connTwo.prepareStatement(sql);
ResultSet rsTwo = psTwo.executeQuery();
while(rsTwo.next()){
System.out.println(rsTwo.getString("username"));
}
connTwo.close();
}
}
输出结果
Howl
-------人工分割线-------
123123
5. 验证
验证Close方法在连接池中是归还给池,而不是关闭
public static void main(String[] args) throws SQLException {
// 循环获取40个连接,输出其地址,然后关闭连接
// 若关闭之后,再获取的连接地址有重复,则说明该连接没有被真正关闭,而是归还到连接池
// 这里的关闭函数被c3p0动态代理了,被改写为放入连接池
for(int i = 0; i < 40;i++) {
Connection conn = C3P0Util.getConnection(1);
System.out.println(conn);
conn.close();
}
}
// 列出部分,可以看到地址重复,验证正确
com.mchange.v2.c3p0.impl.NewProxyConnection@6aaa5eb0 [wrapping: com.mysql.jdbc.JDBC4Connection@50cafc0a]
com.mchange.v2.c3p0.impl.NewProxyConnection@1a407d53 [wrapping: com.mysql.jdbc.JDBC4Connection@68c80b21]
com.mchange.v2.c3p0.impl.NewProxyConnection@5ebec15 [wrapping: com.mysql.jdbc.JDBC4Connection@50cafc0a]