Sonar: quot;Close this PreparedStatementquot;(声纳:“关闭这个 PreparedStatement)
问题描述
如果我在 finally 块中关闭它,为什么 Jenkins 的 SonarQube 插件会抱怨 open 语句?
Why is SonarQube plugin for Jenkins complaining about the open statement if I close it in the finally block?
(我需要在单独的函数中验证数据库连接.)
(I need to validate database connections in a separate function.)
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 的建议以这种方式重构了我的代码,并且 sonar 停止抱怨.
I've refactored my code in this way as suggested by @TT and sonar stopped complaining.
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 仍然抱怨:
Without "try-with-resource" the code could be refactored in the following way but in this case Sonar still complains:
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;
}
                        这篇关于声纳:“关闭这个 PreparedStatement"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:声纳:“关闭这个 PreparedStatement"
				
        
 
            
        - C++ 和 Java 进程之间的共享内存 2022-01-01
 - 如何使用WebFilter实现授权头检查 2022-01-01
 - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 - Jersey REST 客户端:发布多部分数据 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 - Java包名称中单词分隔符的约定是什么? 2022-01-01
 
						
						
						
						
						