I have code like this:
public static MyObject forId(long myObjectId, Connection cxn) throws SQLException {
try (PreparedStatement stmt = cxn.prepareStatement(selectMyObjectById))) {
stmt.setLong(1, myObjectId);
try (ResultSet res = stmt.executeQuery()) {
res.next();
return MyObject.fromResultSet(res);
}
}
}
which SpotBugs identifies as OBL_UNSATISFIED_OBLIGATION
for the JDBC Statement object. Is this a false positive? My impression is that try-with-resources will ensure these resources get closed properly in all cases.
Your ResultSet and PreparedStatement are protected as you rightly state.
If your Connection is also appropriately handled in the relevant scope then yes, it is a false positive.
The scenario in question is definitely a false positive.
There are multiple issues with Spotbugs (as of 3.1.5):
AutoCloseable
objects used within try-with-resources are reported by SpotBugs (false positives):
There's also an issue related specifically to ResultSet
and Statement
("closing a statement implicitly closes the result set obtained from it"):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.