iT邦幫忙

0

java spotbugs

  • 分享至 

  • xImage

我前陣子再測試一段連接伺服器的程式碼,通過spotbugs掃描卻一直跳出這段話。請問該怎麼解決?

try {
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection(url,user,password);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sqlCommand);
            while (rs.next()){
                if(rs.getString("all_students") != null) {
                    resultList.add(0, rs.getString("all_students"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
                try {
                    if (rs != null){
                        rs.close();
                }

                    if (stmt != null){
                        stmt.close();
                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }

                }
        }
Method may fail to clean up stream or resource on checked exception
This method may fail to clean up (close, dispose of) a stream, database object, or other resource requiring an explicit cleanup operation.
In general, if a method opens a stream or other resource, the method should use a try/finally block to ensure that the stream or resource is cleaned up before the method returns.
This bug pattern is essentially the same as the OS_OPEN_STREAM and ODR_OPEN_DATABASE_RESOURCE bug patterns, but is based on a different (and hopefully better) static analysis technique. We are interested is getting feedback about the usefulness of this bug pattern. For sending feedback, check:
contributing guideline
malinglist
In particular, the false-positive suppression heuristics for this bug pattern have not been extensively tuned, so reports about false positives are helpful to us.
See Weimer and Necula, Finding and Preventing Run-Time Error Handling Mistakes, for a description of the analysis technique.
 
Bug kind and pattern: OBL - OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Darwin Watterson
iT邦好手 1 級 ‧ 2020-02-03 15:53:30
最佳解答

問題因該是出在你 finally區塊裡的
第二層try/catch/finally
沒把

rs.close();
stmt.close();
conn.close();

處理好造成的!

我要發表回答

立即登入回答