[M X OBL] Method may fail to clean up stream or resource [OBL_UNSATISFIED_OBLIGATION]
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. To send feedback, either:
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想说明的是:有些资源打开了,但在函数结束的时候却没有关闭。比如:数据库连接......
虽然JAVA有垃圾回收机制,但是,自己打开的资源需要自己手动关闭,否则有可能直到程序退出,JRE才会清理你打开的资源。这里FindBugs建议在try/finally里面关闭打开的资源,在关闭之前,还要判断资源是否为Null,或者再加一层异常捕获处理噢~
以下是一些可能关闭资源失败的例子:
-----------------------------------------------情况1-----------------------------------------
-----------------------------------------------情况2-----------------------------------------
-----------------------------------------------情况3-----------------------------------------