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

  1. <tfoot id='cEuxX'></tfoot>

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

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

      Sonar 要求“使用 try-with-resources 或关闭此“连接".在“终于"中条款.&

      Sonar asks to quot;Use try-with-resources or close this quot;Connectionquot; in a quot;finallyquot; clause.quot;(Sonar 要求“使用 try-with-resources 或关闭此“连接.在“终于中条款.)
            <tbody id='XmFIv'></tbody>
            1. <tfoot id='XmFIv'></tfoot>
            2. <small id='XmFIv'></small><noframes id='XmFIv'>

              <i id='XmFIv'><tr id='XmFIv'><dt id='XmFIv'><q id='XmFIv'><span id='XmFIv'><b id='XmFIv'><form id='XmFIv'><ins id='XmFIv'></ins><ul id='XmFIv'></ul><sub id='XmFIv'></sub></form><legend id='XmFIv'></legend><bdo id='XmFIv'><pre id='XmFIv'><center id='XmFIv'></center></pre></bdo></b><th id='XmFIv'></th></span></q></dt></tr></i><div id='XmFIv'><tfoot id='XmFIv'></tfoot><dl id='XmFIv'><fieldset id='XmFIv'></fieldset></dl></div>
              <legend id='XmFIv'><style id='XmFIv'><dir id='XmFIv'><q id='XmFIv'></q></dir></style></legend>
                <bdo id='XmFIv'></bdo><ul id='XmFIv'></ul>
              • 本文介绍了Sonar 要求“使用 try-with-resources 或关闭此“连接".在“终于"中条款."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想要一个干净的项目.所以我用 Sonar 来检测潜在的缺陷,...

                I want to have a clean project. So I used Sonar to detect potential defects, ...

                在以下方法中,Sonar 要求:使用 try-with-resources 或在finally"子句中关闭此连接"..

                On the below method, Sonar asks to : Use try-with-resources or close this "Connection" in a "finally" clause..

                private Connection createConnection() throws JMSException {
                    MQConnectionFactory mqCF = new MQConnectionFactory();
                    ...
                
                    Connection connection = mqCF.createConnection(...);
                    connection.start();
                
                    return connection;
                }
                

                你能解释一下我做错了什么以及如何避免声纳消息吗?谢谢.

                Can you explain me what I did wrong and how to do to avoid Sonar message? Thank you.

                推荐答案

                在java中,如果你使用FileInptStream, Connection, ResultSet, Input/OutputStream, BufferedReader, PrintWriter等资源 你必须关闭它在垃圾收集发生之前.所以基本上每当连接对象不再使用时,您都必须关闭它.

                In java if you are using resource like FileInptStream, Connection, ResultSet, Input/OutputStream, BufferedReader, PrintWriter you have to close it before garbage collection happens. so basically whenever connection object no longer in use you have to close it.

                试试下面的片段

                Connection c = null;
                    try {
                        c = mqCF.createConnection(...);
                        // do something
                    } catch(SomeException e) {
                        // log exception
                    } finally {
                      try {
                        c.close();
                      } catch(IOException e1){
                        // log something else
                      }
                    }
                
                //try-with-resources
                try(Connection connection = mqCF.createConnection(...)) {
                  //use connection here
                }
                

                在try with resource的情况下连接会被jvm自动关闭,但是Connection接口必须扩展成AutoCloseable/Closable接口.

                In the try with resource case connection will automatically close by jvm, but Connection interface must be extends with AutoCloseable / Closable interface.

                这篇关于Sonar 要求“使用 try-with-resources 或关闭此“连接".在“终于"中条款."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 等)
                  <tbody id='kR5rh'></tbody>

              • <small id='kR5rh'></small><noframes id='kR5rh'>

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