当前位置: 首页 > 面试题库 >

声纳:“关闭此PreparedStatement”

钮安歌
2023-03-14
问题内容

如果我在finally块中将其关闭,为什么Jenkins的SonarQube插件会抱怨open语句?

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

final String PING = "SELECT 1 from dual";

public boolean validateConnection(Connection conn) {

    PreparedStatement statement = 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
            return false;   
        }

        statement = conn.prepareStatement( PING ); //%%%%%% SONAR: Close this "PreparedStatement".
        statement.setQueryTimeout(QUERY_TIMEOUT);

        try( ResultSet rs = statement.executeQuery() ){
            if ( rs != null && rs.next() ) {
                return true;
            }
        }catch(Exception exRs){
            // logger
            throw exRs;
        }
    }catch(Exception ex){
        // logger
    }finally{
        try{
            statement.close();
        }catch(Exception excpt){
            // logger
        }
    }
    return false;
}

问题答案:

我已经按照@TT的建议以这种方式重构了代码,而声纳 不再抱怨

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

没有“ try-with-resource”,代码可以按以下方式重构,但是在这种情况下,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;
}


 类似资料:
  • 我们能比较詹金斯和声纳吗?如果是,怎么做。我想知道詹金斯和声纳的优缺点。比如为什么要使用声纳,它比詹金斯有什么优势,反之亦然?

  • 问题内容: 是否可以为特定的代码块关闭声纳(www.sonarsource.org)测量,而哪些是不想测量的? 一个示例是Findbugs输出的“保留堆栈跟踪”警告。离开服务器时,如果客户端不知道该异常,我可能只想将消息传递回客户端,而不包括我刚刚捕获的实际异常(因为该客户端没有该JAR,例如包含例外)。 问题答案: 这是一个常见问题解答。您可以在线上触发警告。我更喜欢使用FindBugs机制,该

  • 有没有可能关闭sonar(www.sonarsource.org)对特定代码块的测量,哪一块代码不想被测量? Findbugs输出的“保留堆栈跟踪”警告就是一个例子。离开服务器时,如果客户机不知道异常,我可能只想将消息传递回客户机,而不包括刚刚捕获的实际异常(例如,因为客户机没有包含该异常的JAR)。

  • 问题内容: 声纳给出了一个错误,应该将其关闭。我需要修改以下代码才能使用。我该怎么做呢? 问题答案: 当前,代码尚不准备处理异常-您丢失了finally块来关闭打开的流。而且,当然,您是对的-使用try-with-resources解决了这个问题:

  • 持续按住音量 +和音量 -键直到显示(静音)为止。 再次按下音量 +或音量 -键即可恢复原有音量。

  • 如何关闭SonarQube 4中的代码覆盖?我在单元测试中使用了JMockit,在Sonar中使用了JaCoCo代码覆盖插件。它们冲突是因为它们使用不同的JavaAgent来编辑类的字节码(如我所知)。我想关闭声纳中的代码覆盖。我可以在Jacoco的设置中排除我的项目,但这没有帮助。