重用PreparedStatement时可能的资源泄漏? [英] Possible resource leak when reusing PreparedStatement?
问题描述
假设您有以下代码:
Connection connection = null;
PreparedStatement ps = null;
try {
Connection = connectionFactory.getConnection();
ps = statement.prepareStamement(someQuery);
//执行并读取和填充
//现在你想再次使用ps,因为你不想要ps1,ps2,ps3等
ps = statement.prepareStatement(someOtherQuery); //这是否会形成潜在的泄漏?
} catch(很多异常){
//进程异常
} finally {
//关闭资源(使用util类带null检查和一切)
SomeUtilClass.close(ps);
SomeUtilClass.close(connection);
}
是否重复使用ps变量潜在的泄漏?
如果是这样,我不愿意声明多个这样的准备语句(ps1,ps2,ps3等)。
$ b
> 接收几个答案,说明这没关系。我想指出,我遇到打开的游标,保持开放有点太长,并想知道这种模式是否可能与它有关。 我的想法是: 第一个语句被dereferenced和GC'ed,所以第一个PreparedStatement 这不一定会在经典的永久 ,感觉。垃圾收集器最终将得到预处理语句的第一个实例,并调用它的finalizer。 然而,这不是一个好的做法,让垃圾收集器处理释放潜在的关键资源,例如DB句柄:在重用已准备好的语句变量之前应该调用 Suppose you have the following code: Is reusing the ps variable a potential leak? And if so, I would hate to declare multiple of such prepared statements (ps1, ps2, ps3, etc). How should I refactor this? Thoughts anyone? EDIT Receiving several answers stating that this doesn't matter. I would like to point out that I'm experiencing open cursors that stay open a little bit too long, and was wondering if this pattern might have something to do with it. My thoughts were: The first statement gets dereferenced and GC'ed, so how does the first PreparedStatement get closed (Database-wise) in this example? This would not necessarily create a leak in a classic, "permanent", sense. The garbage collector will get to the first instance of the prepared statement eventually, and call its finalizer. However, this is not a good practice to let the garbage collector deal with freeing potentially critical resources, such as DB handles: you should call the
这篇关于重用PreparedStatement时可能的资源泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
close
方法,或者根本不重用该变量。
try {
Connection = connectionFactory.getConnection();
ps = statement.prepareStamement(someQuery);
//执行并读取和填充
//现在你想再次使用ps,因为你不想要ps1,ps2,ps3等
// SomeUtilClass.close(ps); vvvvvvvvvvvv
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
ps = statement.prepareStatement(someOtherQuery); //这是否会形成潜在的泄漏?
} catch(很多异常){
//进程异常
} finally {
//关闭资源(使用util类带null检查和一切)
SomeUtilClass.close(ps);
SomeUtilClass.close(connection);
}
Connection connection = null;
PreparedStatement ps = null;
try {
Connection = connectionFactory.getConnection();
ps = statement.prepareStamement(someQuery);
// execute and read and stuff
// now you want to use the ps again, since you don't want ps1, ps2, ps3, etc.
ps = statement.prepareStatement(someOtherQuery); // DOES THIS FORM A POTENTIAL LEAK?
} catch (a lot of exceptions) {
// process exceptions
} finally {
// close the resources (using util class with null-checks and everything)
SomeUtilClass.close(ps);
SomeUtilClass.close(connection);
}
close
method before reusing the prepared statement variable, or not reuse the variable at all.try {
Connection = connectionFactory.getConnection();
ps = statement.prepareStamement(someQuery);
// execute and read and stuff
// now you want to use the ps again, since you don't want ps1, ps2, ps3, etc.
// v v v v v v v v v v v
SomeUtilClass.close(ps);
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
ps = statement.prepareStatement(someOtherQuery); // DOES THIS FORM A POTENTIAL LEAK?
} catch (a lot of exceptions) {
// process exceptions
} finally {
// close the resources (using util class with null-checks and everything)
SomeUtilClass.close(ps);
SomeUtilClass.close(connection);
}