如何在c3p0中返回连接 [英] how to return a connection in c3p0

查看:230
本文介绍了如何在c3p0中返回连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c3p0 - ComboPooledDataSource。我正在初始化一次,如下所示。

I am using c3p0 - ComboPooledDataSource. I am initializing once as below.

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();
}

但我不确定它是否以正确的方式将连接返回完成执行查询时的池。我想

But i am not sure whether its the right way to return the connection back to the pool when i finish executing a query. I guess the

conn.close()

只返回连接回池而不是真正关闭连接。
我是正确的还是有其他方法吗?请帮助。

just returns the connection back to the pool instead of REALLY CLOSING the connection. Am i correct or is there any other way around? Pls help.

推荐答案

这是初始化代码

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获得连接。

And you get connection from DataSource.

public Connection getConnection() throws SQLException {
    return dataSource.getConnection();
}

要关闭连接,只需调用close()方法。

And to close the connection just call close() method.

connection.close();

这篇关于如何在c3p0中返回连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆