<tfoot id='Re8Fv'></tfoot>
    • <bdo id='Re8Fv'></bdo><ul id='Re8Fv'></ul>

  • <legend id='Re8Fv'><style id='Re8Fv'><dir id='Re8Fv'><q id='Re8Fv'></q></dir></style></legend>

    1. <small id='Re8Fv'></small><noframes id='Re8Fv'>

        <i id='Re8Fv'><tr id='Re8Fv'><dt id='Re8Fv'><q id='Re8Fv'><span id='Re8Fv'><b id='Re8Fv'><form id='Re8Fv'><ins id='Re8Fv'></ins><ul id='Re8Fv'></ul><sub id='Re8Fv'></sub></form><legend id='Re8Fv'></legend><bdo id='Re8Fv'><pre id='Re8Fv'><center id='Re8Fv'></center></pre></bdo></b><th id='Re8Fv'></th></span></q></dt></tr></i><div id='Re8Fv'><tfoot id='Re8Fv'></tfoot><dl id='Re8Fv'><fieldset id='Re8Fv'></fieldset></dl></div>
      1. 声纳:“关闭这个 PreparedStatement"

        Sonar: quot;Close this PreparedStatementquot;(声纳:“关闭这个 PreparedStatement)
        <legend id='RM6BO'><style id='RM6BO'><dir id='RM6BO'><q id='RM6BO'></q></dir></style></legend>

            • <bdo id='RM6BO'></bdo><ul id='RM6BO'></ul>
                <tbody id='RM6BO'></tbody>

              <small id='RM6BO'></small><noframes id='RM6BO'>

              <i id='RM6BO'><tr id='RM6BO'><dt id='RM6BO'><q id='RM6BO'><span id='RM6BO'><b id='RM6BO'><form id='RM6BO'><ins id='RM6BO'></ins><ul id='RM6BO'></ul><sub id='RM6BO'></sub></form><legend id='RM6BO'></legend><bdo id='RM6BO'><pre id='RM6BO'><center id='RM6BO'></center></pre></bdo></b><th id='RM6BO'></th></span></q></dt></tr></i><div id='RM6BO'><tfoot id='RM6BO'></tfoot><dl id='RM6BO'><fieldset id='RM6BO'></fieldset></dl></div>
              1. <tfoot id='RM6BO'></tfoot>
                • 本文介绍了声纳:“关闭这个 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Slf4j LoggerFactory.getLogger and sonarqube(Slf4j LoggerFactory.getLogger 和 sonarqube)
                  Security - Array is stored directly(安全性 - 数组直接存储)
                  SonarQube quot;Class Not Foundquot; during Main AST Scan(SonarQube“找不到类在主 AST 扫描期间)
                  Integrate Spock#39;s test with Sonar(将 Spock 的测试与声纳集成)
                  How do I make Hudson/Jenkins fail if Sonar thresholds are breached?(如果违反声纳阈值,我如何让 Hudson/Jenkins 失败?)
                  automatically add curly brackets to all if/else/for/while etc. in a java code-base(自动将大括号添加到 java 代码库中的所有 if/else/for/while 等)
                  • <legend id='tzXOh'><style id='tzXOh'><dir id='tzXOh'><q id='tzXOh'></q></dir></style></legend>
                    • <small id='tzXOh'></small><noframes id='tzXOh'>

                      <tfoot id='tzXOh'></tfoot>
                      <i id='tzXOh'><tr id='tzXOh'><dt id='tzXOh'><q id='tzXOh'><span id='tzXOh'><b id='tzXOh'><form id='tzXOh'><ins id='tzXOh'></ins><ul id='tzXOh'></ul><sub id='tzXOh'></sub></form><legend id='tzXOh'></legend><bdo id='tzXOh'><pre id='tzXOh'><center id='tzXOh'></center></pre></bdo></b><th id='tzXOh'></th></span></q></dt></tr></i><div id='tzXOh'><tfoot id='tzXOh'></tfoot><dl id='tzXOh'><fieldset id='tzXOh'></fieldset></dl></div>

                        • <bdo id='tzXOh'></bdo><ul id='tzXOh'></ul>

                              <tbody id='tzXOh'></tbody>