假设你有下面的代码:重复使用PreparedStatement时可能发生资源泄漏?
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的获得在这个例子中关闭(数据库明智)?
+1为队列的想法 – bknopper