我正在使用 c3p0 - ComboPooledDataSource。我如下初始化一次。
private void init() {
cpds = new ComboPooledDataSource();
cpds.setDriverClass(driverName);
cpds.setJdbcUrl(url);
cpds.setUser(userName);
cpds.setPassword(pwd);
}
我从池中获得连接,如下所示
public synchronized Connection getLocalConnection(String ipAddr)
throws SQLException {
return cpds.getConnection();
}
但我不确定当我完成执行查询时将连接返回到池是否是正确的方法。我猜是
conn.close()
只是将连接返回到池中,而不是真正关闭连接。
我是正确的还是有其他方法?请帮忙。
这是初始化代码
private DataSource init() {
DataSource unpooled = DataSources.unpooledDataSource(DB_URL, DB_USERNAME, DB_PASSWORD);
Map<String, Object> overrideProps = new HashMap<String, Object>();
overrideProps.put("maxPoolSize", MAX_POOL_SIZE);
overrideProps.put("minPoolSize", MIN_POOL_SIZE);
return DataSources.pooledDataSource(unpooled, overrideProps);
}
然后您就可以从 DataSource 获得连接。
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
要关闭连接只需调用 close() 方法。
connection.close();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)