c3p0连接池

突然想用一下c3p0连接池,在网上找了一下,要么根本不能用,要么就是达不到自己的要求。
不过后来想起以前有人写过这样的一个类,于是拿出来试了一下,果然能用,而且功能也非常完善。

环境:
eclipse3.5
mysql-connector-java-5.1.7-bin.jar
c3p0-0.9.1.2.jar
commons-logging-1.1.1.jar


JAVA类代码如下:


public class ConnectionPool {

public static void cleanUp(Connection con) {
_instance._cleanUp(con);
}
public static void cleanUp(Connection con, Statement s) {
_instance._cleanUp(con, s);
}
public static void cleanUp(Connection con, Statement s, ResultSet rs) {
_instance._cleanUp(con, s, rs);
}
public static void destroy() throws SQLException {
_instance._destroy();
}

public static Connection getConnection() throws SQLException {
return _instance._getConnection();
}

public static Properties getProperties() {
return _instance._props;
}

private ConnectionPool() {
try {

// Properties

ClassLoader classLoader = getClass().getClassLoader();

_props = new Properties();

_props.load(classLoader.getResourceAsStream(
"connection-pool.properties"));

_props.list(System.out);

// Pooled data source

String driverClass = _props.getProperty("driver.class");
String jdbcUrl = _props.getProperty("jdbc.url");
String user = _props.getProperty("user");
String password = _props.getProperty("password");

int minPoolSize = 5;

try {
minPoolSize = Integer.parseInt(
_props.getProperty("min.pool.size"));
}
catch (Exception e) {
}

int maxPoolSize = 5;

try {
maxPoolSize = Integer.parseInt(
_props.getProperty("max.pool.size"));
}
catch (Exception e) {
}

int acquireIncrement = 5;

try {
acquireIncrement = Integer.parseInt(
_props.getProperty("acquire.increment"));
}
catch (Exception e) {
}

_cpds = new ComboPooledDataSource();

_cpds.setDriverClass(driverClass);
_cpds.setJdbcUrl(jdbcUrl);
_cpds.setUser(user);
_cpds.setPassword(password);

_cpds.setMinPoolSize(minPoolSize);
_cpds.setMaxPoolSize(maxPoolSize);
_cpds.setAcquireIncrement(acquireIncrement);
}
catch (Exception e) {
_log.error(e);
}
}

private void _cleanUp(Connection con) {
_cleanUp(con, null, null);
}

private void _cleanUp(Connection con, Statement s) {
_cleanUp(con, s, null);
}

private void _cleanUp(Connection con, Statement s, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
}
catch (SQLException sqle) {
_log.error(sqle);
}

try {
if (s != null) {
s.close();
}
}
catch (SQLException sqle) {
_log.error(sqle);
}

try {
if (con != null) {
con.close();
}
}
catch (SQLException sqle) {
_log.error(sqle);
}
}

private void _destroy() throws SQLException {
DataSources.destroy(_cpds);
}

private Connection _getConnection() throws SQLException {
return _cpds.getConnection();
}

private static Log _log = LogFactory.getLog(ConnectionPool.class);

private static ConnectionPool _instance = new ConnectionPool();

private Properties _props;
private ComboPooledDataSource _cpds;

}


当然还得在包的根目录下建一个配置文件connection-pool.properties,内容如下:

driver.class=com.mysql.jdbc.Driver
jdbc.url=
user=
password=

min.pool.size=5
max.pool.size=20
acquire.increment=5

在配置文件中填上相应的值,以后就可以直接使用此类获得数据库连接了。需要注意的是用完之后要手动关闭连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值