<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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  • <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>