Java 声纳:;“关闭此准备好的报表”;

Java 声纳:;“关闭此准备好的报表”;,java,sonarqube,Java,Sonarqube,如果我在finally块中关闭open语句,为什么Jenkins插件会抱怨它 (我需要在单独的函数中验证数据库连接。) 我按照@TT的建议重构了代码,sonar停止抱怨 public boolean validateConnection(Connection conn) { LOGGER.log( LogEntries.PingConn ); try{ if(conn == null){ LOGGER.log( LogEntries.

如果我在finally块中关闭open语句,为什么Jenkins插件会抱怨它

(我需要在单独的函数中验证数据库连接。)


我按照@TT的建议重构了代码,sonar停止抱怨

public boolean validateConnection(Connection conn) {

    LOGGER.log( LogEntries.PingConn );

    try{

        if(conn == null){
            LOGGER.log( LogEntries.PingError, "Null connection on PING. Reached max # of connections or network issue. Stats: "+getCacheStatistics() );
            return false;
        }

        if(conn.isClosed()){
            LOGGER.log( LogEntries.PingError, "Found closed connection during validation PING." );
            return false;   
        } 

        try( PreparedStatement statement = conn.prepareStatement( PING ) ){

             statement.setQueryTimeout(QUERY_TIMEOUT);

             try( ResultSet rs = statement.executeQuery() ){

                if ( rs != null && rs.next() ) {
                    return true;
                }
            }
        }

    }catch(Exception ex){
        LOGGER.log( LogEntries.PingError, ex );
    }

    return false;
}
如果没有“资源试用”,代码可以按以下方式重构,但在这种情况下Sonar仍会抱怨:

public boolean validateConnection(Connection conn) {

    LOGGER.log( LogEntries.PingConn );

    PreparedStatement statement = null;
    ResultSet rs = null;
    try{

        if(conn == null){
            LOGGER.log( LogEntries.PingError, "Null connection on PING. Reached max # of connections or network issue. Stats: "+getCacheStatistics() );
            return false;
        }

        if(conn.isClosed()){
            LOGGER.log( LogEntries.PingError, "Found closed connection during validation PING." );
            return false;   
        } 

        statement = conn.prepareStatement( PING );
        statement.setQueryTimeout( QUERY_TIMEOUT );
        rs = statement.executeQuery();

        if ( rs != null && rs.next() ) {
            return true;
        }

    }catch(Exception ex){
        LOGGER.log( LogEntries.PingError, ex );
    }finally{
        try {
            if(rs!=null){
                rs.close();
            }
        } catch (SQLException eClosing1) {
            LOGGER.log( LogEntries.PingError, eClosing1 );
        }finally{
            try {
                if(statement!=null){
                    statement.close();
                }
            }catch (SQLException eClosing2) {
                LOGGER.log( LogEntries.PingError, eClosing2 );
            }   
        }
     }

    return false;
}

我看不出把所有这些嵌套的
try
块放在没有
catch
的地方有什么意义。
您只需在第一次
尝试
时使用合适的
捕获
最后
关闭
状态
连接
,然后根据
null

检查它们。如果conn为null或关闭,您更新代码的方式将遇到空指针异常。对。。但我在关闭语句之前删除了“if(statement!=null)”,因为sonar抱怨该条件始终为真..@TT。我还不清楚否决投票的原因,你能澄清一下吗?谢谢我是唯一一个投赞成票的人,另外两个人投了反对票。我不能代表其他人说话。我不使用SonarQube,但我会说:开始使用(需要Java 1.7+)。也许那会让你闭嘴。另外,为什么要在try块中检查
conn
?把他们移到那个街区之外。这是一种尝试资源的方法()。在我的问题中,代码背后的想法是使用不同的日志条目,具有不同的严重性,但Sonar奇怪地抱怨未关闭的声明。我已经更新了我的答案,添加了你的解决方案,但Sonar仍然抱怨你的方法。
public boolean validateConnection(Connection conn) {

    LOGGER.log( LogEntries.PingConn );

    PreparedStatement statement = null;
    ResultSet rs = null;
    try{

        if(conn == null){
            LOGGER.log( LogEntries.PingError, "Null connection on PING. Reached max # of connections or network issue. Stats: "+getCacheStatistics() );
            return false;
        }

        if(conn.isClosed()){
            LOGGER.log( LogEntries.PingError, "Found closed connection during validation PING." );
            return false;   
        } 

        statement = conn.prepareStatement( PING );
        statement.setQueryTimeout( QUERY_TIMEOUT );
        rs = statement.executeQuery();

        if ( rs != null && rs.next() ) {
            return true;
        }

    }catch(Exception ex){
        LOGGER.log( LogEntries.PingError, ex );
    }finally{
        try {
            if(rs!=null){
                rs.close();
            }
        } catch (SQLException eClosing1) {
            LOGGER.log( LogEntries.PingError, eClosing1 );
        }finally{
            try {
                if(statement!=null){
                    statement.close();
                }
            }catch (SQLException eClosing2) {
                LOGGER.log( LogEntries.PingError, eClosing2 );
            }   
        }
     }

    return false;
}