在src目录下新建一个名叫 c3p0-config.xml 的文件,注意,必须是这个文件名。
<c3p0-config>
<default-config>
<property name="initialPoolSize">10property>
<property name="maxIdleTime">30property>
<property name="maxPoolSize">100property>
<property name="minPoolSize">10property>
<property name="maxStatements">200property>
default-config>
<named-config name="mysql">
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/CoupleSpaceproperty>
<property name="user">rootproperty>
<property name="password">rootproperty>
<property name="initialPoolSize">10property>
<property name="maxIdleTime">30property>
<property name="maxPoolSize">100property>
<property name="minPoolSize">10property>
<property name="maxStatements">200property>
named-config>
......
......
......
c3p0-config>
还可以配置其他属性
<property name="acquireIncrement">3property>
<property name="acquireRetryAttempts">30property>
<property name="acquireRetryDelay">1000property>
<property name="autoCommitOnClose">falseproperty>
<property name="automaticTestTable">Testproperty>
<property name="breakAfterAcquireFailure">falseproperty>
<property name="checkoutTimeout">100property>
<property name="connectionTesterClassName">property>
<property name="factoryClassLocation">nullproperty>
<property name="forceIgnoreUnresolvedTransactions">falseproperty>
<property name="idleConnectionTestPeriod">60property>
<property name="initialPoolSize">3property>
<property name="maxIdleTime">60property>
<property name="maxPoolSize">15property>
<property name="maxStatements">100property>
<property name="maxStatementsPerConnection">property>
<property name="numHelperThreads">3property>
<property name="overrideDefaultUser">rootproperty>
<property name="overrideDefaultPassword">passwordproperty>
<property name="password">property>
<property name="preferredTestQuery">select id from test where id=1property>
<property name="propertyCycle">300property>
<property name="testConnectionOnCheckout">falseproperty>
<property name="testConnectionOnCheckin">trueproperty>
<property name="user">rootproperty>
<property name="usesTraditionalReflectiveProxies">falseproperty>
创建工具类,定义获取connection、释放connection的方法
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Utils {
//通过标识名来创建相应连接池
static ComboPooledDataSource dataSource=new ComboPooledDataSource();
//从连接池中取用一个连接
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (Exception e) {
throw new CustomException(ExceptionList.FAILURE);
}
}
//释放连接回连接池
public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
throw new CustomException(ExceptionList.FAILURE);
}
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
throw new CustomException(ExceptionList.FAILURE);
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
throw new CustomException(ExceptionList.FAILURE);
}
}
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBUtil_BO {
public Connection conn = null;
public PreparedStatement st = null;
public ResultSet rs = null;
public DBUtil_BO() {
super();
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {
private static void realseSource(Connection _conn, PreparedStatement _st, ResultSet _rs) {
C3p0Utils.close(_conn, _st, _rs);
}
public static void realseSource(DBUtil_BO _vo) {
if (_vo != null) {
realseSource(_vo.conn, _vo.st, _vo.rs);
}
}
// 注意:查询操作完成后,因为还需提取结果集中信息,所以仍保持连接,在结果集使用完后才通过DBUtils.realseSource()手动释放连接
public static ResultSet executeQuery(DBUtil_BO vo) {
try {
vo.rs = vo.st.executeQuery();
} catch (SQLException e) {
realseSource(vo);
throw new CustomException(ExceptionList.FAILURE);
}
return vo.rs;
}
// 而update操作完成后就可以直接释放连接了,所以在方法末尾直接调用了realseSourse()
public static void executeUpdate(DBUtil_BO vo) {
Connection conn = vo.conn;
PreparedStatement st = vo.st;
try {
st.executeUpdate();
} catch (SQLException e) {
realseSource(conn, st, null);
throw new CustomException(ExceptionList.FAILURE);
}
realseSource(conn, st, null);
}
}
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBHelper {
private static DBUtil_BO dbBo1;
/**
* 查询
*
* @param sql
* @param obj
* @return
* @throws SQLException
*/
public static ResultSet select(String sql, Object... obj) throws Exception {
if (null != dbBo1) {
DBUtils.realseSource(dbBo1);
}
DBUtil_BO dbBo = new DBUtil_BO();
dbBo.conn = C3p0Utils.getConnection();
dbBo.st = dbBo.conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
dbBo.st.setObject(i + 1, obj[i]);
}
ResultSet rs = DBUtils.executeQuery(dbBo);
dbBo1 = dbBo;
return rs;
}
/**
* 修改
*
* @param sql
* @param obj
* @return
* @throws Exception
*/
public static void update(String sql, Object... obj) throws Exception {
DBUtil_BO dbBo = new DBUtil_BO();
dbBo.conn = C3p0Utils.getConnection();
dbBo.st = dbBo.conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
dbBo.st.setObject(i + 1, obj[i]);
}
DBUtils.executeUpdate(dbBo);
}
}
String selectSql = "SELECT USER_ID_ FROM act_hi_comment WHERE TASK_ID_ = ( SELECT ID_ FROM act_hi_taskinst WHERE PROC_INST_ID_ = ? AND NAME_ = ? AND ASSIGNEE_ = ? )";
rs = DBHelper.select(selectSql, procInstId, sysRole.getRoleName(), sysUser.getId());
rs.next();
这样很简单的实现数据库连接池的管理。