gpt4 book ai didi

java - SpotBugs 是否报告了未在此处关闭资源的误报?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:46 25 4
gpt4 key购买 nike

我有这样的代码:

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);
}
}
}

对于 JDBC 语句对象,SpotBugs 将其标识为 OBL_UNSATISFIED_OBLIGATION。这是误报吗?我的印象是 try-with-resources 将确保这些资源在所有情况下都能正确关闭。

最佳答案

正如您正确声明的那样,您的 ResultSet 和 PreparedStatement 受到保护。

如果您的连接也在相关范围内得到适当处理,那么是的,这是误报。

关于java - SpotBugs 是否报告了未在此处关闭资源的误报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47776786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com