【发布时间】:2014-04-01 12:40:38
【问题描述】:
假设你有以下代码:
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);
}
重用 ps 变量是否存在潜在泄漏?
如果是这样,我不想声明多个此类准备好的语句(ps1、ps2、ps3 等)。我应该如何重构它?
有人想吗?
编辑
收到几个回答说这无关紧要。我想指出,我遇到的打开游标保持打开的时间有点过长,我想知道这种模式是否与它有关。
我的想法是:
第一个语句被取消引用并被 GC'ed,那么在这个例子中第一个 PreparedStatement 是如何关闭的(数据库方面)?
【问题讨论】:
标签: java database jdbc resources