重用PreparedStatement
我在我们的代码库上运行了 findbugs,它指出还有两个语句仍然需要关闭。 在这部分代码中,我们运行:
preparedStatement = connection.prepareStatement(query);
对于 3 个不同的查询,重用 preparedStatement。 在finally块中,我们确实关闭了资源:
finally{
try{
if (resultSet != null)
resultSet.close();
} catch (Exception e) {
exceptionHandler.ignore(e);
}
try {
if (preparedStatement != null)
preparedStatement.close();
} catch(Exception e) {
exceptionHandler.ignore(e);
}
是否应该在下一个connection.prepareStatement(query)之前关闭该语句; 还是 findbugs 很谨慎?
I ran findbugs on our code base and it pointed out there are two more Statements that still need to be closed. In this section of the code we run:
preparedStatement = connection.prepareStatement(query);
for 3 different queries, reusing preparedStatement. In the finally block we do close the resource:
finally{
try{
if (resultSet != null)
resultSet.close();
} catch (Exception e) {
exceptionHandler.ignore(e);
}
try {
if (preparedStatement != null)
preparedStatement.close();
} catch(Exception e) {
exceptionHandler.ignore(e);
}
Should the statement be closed before the next connection.prepareStatement(query); or is this findbugs being cautious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,在执行下一个connection.prepareStatement之前必须关闭该语句。 否则,您将丢失对未关闭的前一个(又称泄漏声明)的引用。 在每个语句使用周围包裹一个 try {}finally{},并在finally中将其关闭。
Yes, the statement must be closed before you perform the next connection.prepareStatement. Otherwise, you're losing your reference to the un-closed previous one (aka leaking statements). Wrap a try {} finally {} around each statement use, closing it in the finally.