日期:2014-05-19 浏览次数:20663 次
/**
* @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
* @功能描述:连接池接口
* @Since Mar 11, 2011
*
*/
public interface PooledConnection {
/**
*
* @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
* @功能描述:初始化
* @Since Mar 11, 2011
*/
public void init();
/**
*
* @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
* @功能描述:关闭连接池
* @Since Mar 11, 2011
*/
public void destory();
/**
*
* @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
* @功能描述:获取连接
* @Since Mar 11, 2011
* @return
* @throws SQLException
*/
public Connection getConnection() throws SQLException;
public void releaseConnection(Connection conn) throws SQLException;
}
/**
* @author <a href="mailto:amoszhou@foxmail.com">大胡子</a>
* @功能描述:基于C3PO连接池的实现类
* @Since Mar 11, 2011
*
*/
public class RYTPooledConnection implements PooledConnection {
/**
* c3po的数据源
*/
private static ComboPooledDataSource ds;
/**
* 关闭数据源
*/
public final void destory() {
ds.close();
}
/**
* 获取数据库连接
*/
public final Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 初始化
*/
public final void init() {
if(null == ds){
ds = new ComboPooledDataSource();
}
}
public void releaseConnection(Connection conn) throws SQLException {
if(null != conn){
conn.close();
}
}
}
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/testdb
c3p0.user=root
c3p0.password=123456
c3p0.initialPoolSize=10
c3p0.minPoolSize=10
c3p0.maxPoolSize=100
public class Transaction {
/**database connection**/
private Connection connection;