Java关闭连接和findbug [英] Java closing connections and findbugs

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

问题描述

在我们的代码中,我们通常使用以下模式:

  
try {
conn = getConnection();
// do databasey stuff
} catch(抛出的异常){
} finally {
try {
conn.close();
} catch(SQLException ex){
logger.error(无法清除数据库连接,ex);
}
}

但是findbugs不喜欢这样。因为conn.close()可以抛出异常,那么连接不能保证被关闭。
findbugs太拙劣,或者有更好的方式来关闭数据库连接。



编辑:
添加删除try catch around close。你真正想做的是将精英绅士的答案与 @结合起来, edu.umd.cs.findbugs.annotations.SuppressWarnings(OBL_UNSATISFIED_OBLIGATION)注释。 FindBugs似乎只有在以下方式完成关闭方法(这是btw的首选顺序为这样做)才会很高兴:

  ... 
} finally {
try {
resultSet.close();
} catch(SqlException e){
//日志错误
} finally {
try {
statement.close();
} catch(SqlException e){
//日志错误
} finally {
try {
connection.close();
} catch(SqlException e){
//日志错误
}
}
}
}

这是非常详细的,你可能不想做,如果没有其他原因比你的carpal隧道的爱,因此你应该使用 DBUtils.closeQuietly()方法(或创建自己的,您的调用)。但是,FindBugs不能识别这(即使用库或您自己的方法)正确关闭资源和问题警告。在这种情况下,显然是假阳性。因此,必须确保它是您获得的唯一警告,然后禁用该方法的特定警告。

  @ edu.umd。 cs.findbugs.annotations.SuppressWarnings(OBL_UNSATISFIED_OBLIGATION)
public void doStuff(final Connection connection){
try {
// Do databasey stuff
} catch(SqlException e) {
// throw a glorious exception ....
} finally {
DbUtils.closeQuietly(resultSet);
DbUtils.closeQuietly(statement);
DbUtils.closeQuietly(connection);
}

这样,用少数几行代码清理资源, FindBugs警告。


In our code we usually use the following pattern:

Connection conn;
try{
    conn = getConnection();
    //Do databasey stuff
}catch(Exceptions that get thrown){
}finally{
    try{
        conn.close();
    }catch(SQLException ex){
        logger.error("Failed to cleanup database connection",ex);
    }
}

However findbugs doesn't like this. Since conn.close() can throw an exception then the connection isn't guaranteed to be closed. Is findbugs being too pedantic or is there a better way to close database connections.

Edit: Added removed try catch around close.

解决方案

What you really want to be doing is to combine "The Elite Gentleman"'s answer with the @edu.umd.cs.findbugs.annotations.SuppressWarnings( "OBL_UNSATISFIED_OBLIGATION" ) annotation. FindBugs seems to only be happy if you the complete closing in method in the following manner (which is btw the preferred sequence for doing so):

...
}finally{
    try{ 
       resultSet.close();
    }catch( SqlException e ){
       //log error
    }finally{
       try{
          statement.close();
       }catch( SqlException e ){
          //log error
       }finally{
          try{
              connection.close();
          }catch( SqlException e ){
              //log error
          }
       }
    }
}

Which is very verbose and you probably don't want to do if for no other reason than the love of your carpal tunnel, thus you should use the DBUtils.closeQuietly() method (or create your own, your call). However, FindBugs doesn't recognise this (i.e. using the library or your own method) as properly closing the resources and issues you a warning. In this case it is clearly a false positive. Therefore must make sure its the only warning you are getting and then disable that specific warning for that method.

@edu.umd.cs.findbugs.annotations.SuppressWarnings( "OBL_UNSATISFIED_OBLIGATION" )
public void doStuff( final Connection connection ){
    try{
        //Do databasey stuff
    }catch( SqlException e ){
        //throw a glorious exception....
    }finally{
        DbUtils.closeQuietly( resultSet  );
        DbUtils.closeQuietly( statement  );
        DbUtils.closeQuietly( connection );
}

This way you clean up your resources with the few lines of code and avoid a FindBugs warning.

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

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