问题描述
我有连接泄漏到DB在我的代码。
有趣的是,当我调试,所有的连接都成功关闭(或当我做Thread.Sleep(100))。但没有那总是有一个连接保持!
你能告诉这里的问题是什么吗?
ComboPooledDataSource dataSource = null;
try {
dataSource = dataSourceFactory.getDataSource(dbType,dbProps);
dataSource.getConnection();
} finally {
if(dataSource!= null)
{
try
{
log.debug(validate关闭SQL连接池);
DataSources.destroy(dataSource);
dataSource = null;
log.debug(validate():SQL连接池已关闭);
}
catch(Exception e)
{
log.error(validate():关闭数据源时出错,e)
}
}
}
我认为您的问题与关于C3P0的有关。我想在之前的
解决您的问题。我也猜想你知道一些连接已经保持不变检查你的MySQL日志。然而,除了在你的情况下,我建议手动关闭连接,除了数据源,这是在每次使用它后做的事情。所以我建议以下修改: Thread.sleep(delay)
DataSources.destroy(dataSource)
ComboPooledDataSource dataSource = null;
Connection connection = null;
try {
dataSource = dataSourceFactory.getDataSource(dbType,dbProps);
//从数据源获取连接
connection = dataSource.getConnection();
} finally {
if(connection!= null){
connection.close();
}
if(dataSource!= null){
try {
log.debug(validate():Closing SQL connection pool);
DataSources.destroy(dataSource);
dataSource = null;
log.debug(validate():SQL连接池已关闭);
} catch(Exception e){
log.error(validate():Error closing data source,e);
}¥b $ b}
}
I have connection leak to DB in my code.The funny thing is that when I debug, all the connections are closed successfully (or when I do Thread.Sleep(100) ). but without that there is always one connection that stays!Can you tell what is the problem here?
ComboPooledDataSource dataSource = null;
try {
dataSource = dataSourceFactory.getDataSource(dbType, dbProps);
dataSource.getConnection();
} finally {
if (dataSource != null)
{
try
{
log.debug("validate() : Closing SQL connection pool");
DataSources.destroy(dataSource);
dataSource = null;
log.debug("validate() : SQL connection pool is closed");
}
catch (Exception e)
{
log.error("validate() : Error closing data source", e);
}
}
}
I think that your problem is related to this question regarding C3P0. I guess a Thread.sleep(delay)
before DataSources.destroy(dataSource)
solves your problem. I also guess that you know that some connection has been left intact checking your MySQL logs. However, apart from that in your case I would suggest to manually close the connection apart from the datasource which is something to do after every use of it. So I would suggest the following modification:
ComboPooledDataSource dataSource = null;
Connection connection = null;
try {
dataSource = dataSourceFactory.getDataSource(dbType, dbProps);
// Get a connection from the datasource
connection = dataSource.getConnection();
} finally {
if (connection!=null){
connection.close();
}
if (dataSource != null) {
try {
log.debug("validate() : Closing SQL connection pool");
DataSources.destroy(dataSource);
dataSource = null;
log.debug("validate() : SQL connection pool is closed");
} catch (Exception e) {
log.error("validate() : Error closing data source", e);
}
}
}
这篇关于与DB的关闭连接不会关闭所有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!