<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://172.26.3.250:3306/csms?useUnicode=true&characterEncoding=utf-8
</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
</c3p0-config>
c3p0.jdbcUrl=jdbc:mysql://172.26.3.250:3306/csms?useUnicode=true&characterEncoding=utf-8
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.user=root
c3p0.password=root
#连接池中保留的最小连接数
c3p0.minPoolSize=5
#连接池中保留的最大连接数。Default: 15
c3p0.maxPoolSize=30
#初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3
c3p0.initialPoolSize=10
#最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0
c3p0.maxIdleTime=60
#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3
c3p0.acquireIncrement=5
#JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
#属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
#如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
c3p0.maxStatements=0
#每60秒检查所有连接池中的空闲连接。Default: 0 -->
c3p0.idleConnectionTestPeriod=60
#定义在从数据库获取新连接失败后重复尝试的次数。Default: 30
c3p0.acquireRetryAttempts=30
#获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
#保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
#获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
c3p0.breakAfterAcquireFailure=true
#因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
#时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
#等方法来提升连接测试的性能。Default: false -->
c3p0.testConnectionOnCheckout=false
使用这两种方式进行配置时,只要将配置好的文件放入classpath文件夹下即可,在java代码当中不用显示的给出访问配置方式的代码,c3p0会自动识别!
C3P0Utils.java:
package com.c3p0.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Utils {
private static DataSource ds;
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); // map
static {
ds = new ComboPooledDataSource();//直接使用即可,不用显示的配置,其会自动识别配置文件
}
public static DataSource getDataSource() {
return ds;
}
public static Connection getConnection() throws SQLException {
try {
// 得到当前线程上绑定的连接
Connection conn = tl.get();
if (conn == null) { // 代表线程上没有绑定连接
conn = ds.getConnection();
tl.set(conn);
}
return conn;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void startTransaction() {
try {
// 得到当前线程上绑定连接开启事务
Connection conn = tl.get();
if (conn == null) { // 代表线程上没有绑定连接
conn = ds.getConnection();
tl.set(conn);
}
conn.setAutoCommit(false);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void commitTransaction() {
try {
Connection conn = tl.get();
if (conn != null) {
conn.commit();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void closeConnection() {
try {
Connection conn = tl.get();
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
tl.remove(); // 千万注意,解除当前线程上绑定的链接(从threadlocal容器中移除对应当前线程的链接)
}
}
}
ConnectionDemo.java:
package com.c3p0;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.c3p0.util.C3P0Utils;
public class ConnectionDemo {
public static void main(String[] args) throws SQLException {
System.out.println("使用连接池................................");
for (int i = 0; i < 20; i++) {
long beginTime = System.currentTimeMillis();
Connection conn = C3P0Utils.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT count(*) as a FROM cs_ms_common_questions");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
//TODO
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
C3P0Utils.closeConnection();
}
long endTime = System.currentTimeMillis();
System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime));
}
System.out.println("不使用连接池................................");
for (int i = 0; i < 20; i++) {
long beginTime = System.currentTimeMillis();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
Connection conn = DriverManager.getConnection("jdbc:mysql://172.26.3.250:3306/csms?useUnicode=true&characterEncoding=utf-8", "root","root");
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT count(*) as a FROM cs_ms_common_questions");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
//TODO
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime));
}
}
}
在不使用连接池时,每次花费的时间都比较长。