gpt4 book ai didi

java - 重用 PreparedStatement

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:18:55 25 4
gpt4 key购买 nike

我在我们的代码库上运行了 findbugs,它指出还有两个语句仍需要关闭。在这部分代码中,我们运行:

preparedStatement = connection.prepareStatement(query);

对于 3 个不同的查询,重用 preparedStatement。在 finally block 中,我们确实关闭了资源:

finally{
try{
if (resultSet != null)
resultSet.close();
} catch (Exception e) {
exceptionHandler.ignore(e);
}
try {
if (preparedStatement != null)
preparedStatement.close();
} catch(Exception e) {
exceptionHandler.ignore(e);
}

是否应该在下一个connection.prepareStatement(query)之前关闭语句;还是这个 findbugs 很谨慎?

最佳答案

是的,在执行下一个 connection.prepareStatement 之前必须关闭该语句。否则,您将丢失对未关闭的前一个(也称为泄漏语句)的引用。在每个语句 use 周围包装一个 try {} finally {},在 finally 中将其关闭。

关于java - 重用 PreparedStatement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1018123/

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