语句和结果集在连接关闭后关闭 [英] Statement and Resultset close after connection close

查看:88
本文介绍了语句和结果集在连接关闭后关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近搬到一个项目,在那里我遇到了很多这种性质的代码-(这是使用jdbc postgres驱动程序的)

I have recently moved to a project where I am encountering a lot of code of this nature - (This is using the jdbc postgres driver)

try {
    Connection conn = pool.getAConnection(); //home-grown conn pool 
    PreparedStatement ps = ..;
    ResultSet rs = ..;
    rs = ps.executeQuery();
    ...
 } catch (SQLException se) {
    conn.close(); 
} finally {
    if (stmt != null) stmt.close();
    if (rs != null) rs.close();
}

显然,此代码已经生产了一段时间,没有引起问题.

Apparently this code has been in production for a while, without causing issues.

我发现很难理解的是,在异常流中,连接首先被关闭或返回到池中.然后尝试关闭语句和结果集.在关闭父连接对象之后执行此操作是否有意义?

What I find hard to understand is, in the exception flow, the connection gets closed or returned to the pool first; and then the statement and resultset are attempted to be closed. Does it make sense to execute this after the parent connection object is closed ?

由于代码的结构方式,必须在异常块中完成连接释放.那是无法改变的.话虽这么说,在将连接释放到池中之后,可以最后将stmt.close()和rs.close()保留吗?

Because of the way the code is structured, connection release has to be done in the exception block. That cannot be changed. That being said, is it okay to leave the stmt.close() and rs.close() in finally after the connection has been released to the pool ?

为进一步澄清,如果我的理解是正确的(即,必须在关闭连接之前而不是之后关闭语句和结果集),则需要在catch和final之间重复一些代码.现在,修改后的代码如下所示.可以简化吗?

To clarify further, if my understanding is correct (i.e., statement and resultset must be closed before connection close and not after), I need to repeat some code between the catch and finally. The revised code now looks as below. Can this be simplified ?

try {
...
} catch(Exception ex){
      if (rs != null) {
         close(rs); rs = null; // close() method impl just calls rs.close() in try-catch block
      }
      if (ps != null) {
         close(ps); ps = null;
      }
      processException( ex, con); // This method logs and then either closes the connection or releases to pool, depending on some conditions. 
      con = null;
  } finally {
      if (rs != null) {
          close(rs); 
      }
      if (ps != null) {
          close(ps); 
      }             
      if (null != con) {
          close(con);
      }
  }

仅从角度来看,此代码已满-至少有100个左右的方法!如果可能的话,我想进一步简化一下.感谢您的反馈.

Just for perspective, this code is all over - at least a 100 or so methods ! I would like to simplify this further if possible. Appreciate your feedback.

推荐答案

对于在 finally 块中释放连接非常有意义.在 finally 块中关闭 Statement ResultSet 的操作也是如此.

It makes perfect sense for connections to be released in the finally block. And so does closing your Statement and ResultSet in your finally block.

推理很简单:在成功执行和异常情况下,您都要确保关闭 Statement ResultSet .连接也一样.我会在 finally

The reasoning is simple: You're making sure that your Statement and ResultSet gets closed, in both successful execution and exception scenario. The same goes for connection. I would've done something like this in the finally block

try{

}catch(Exception exe){

}finally{
    if (stmt != null) stmt.close();
    if (rs != null) rs.close();

    //release connection to connection pool

}

此外,我相信,当关闭 Statement 时,其当前的 ResultSet 也将关闭.因此,如果 rs stmt 相关联,那么我相信当您执行 stmt.close()

Also, I believe that when a Statement is closed, its current ResultSet is also closed. So in case rs is associated with stmt, then I believe it would be closed when you execute stmt.close()

这篇关于语句和结果集在连接关闭后关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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