重用PreparedStatement [英] Reusing a PreparedStatement

查看:95
本文介绍了重用PreparedStatement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我们的代码库中运行了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);

用于3个不同的查询,重用prepareStatement.在finally块中,我们确实关闭了资源:

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);
   }

应该在下一个连接之前关闭该语句.还是这个findbug谨慎?

Should the statement be closed before the next connection.prepareStatement(query); or is this findbugs being cautious?

推荐答案

是的,必须在执行下一个connection.prepareStatement之前关闭该语句.否则,您将丢失对未关闭的前一个引用(又称泄漏语句).将每个语句的使用情况都尝试一下{}最终{},然后在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.

这篇关于重用PreparedStatement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆