PreparedStatementの再利用
-
06-07-2019 - |
質問
コードベースでfindbugsを実行しましたが、閉じる必要があるステートメントがさらに2つあることがわかりました。コードのこのセクションで実行します:
preparedStatement = connection.prepareStatement(query);
3つの異なるクエリに対して、preparedStatementを再利用します。 finallyブロックでリソースを閉じます:
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を実行する前にステートメントを閉じる必要があります。そうしないと、閉じられていない前の参照(リークステートメントとも呼ばれます)への参照が失われます。 try {} finally {}をステートメントの使用ごとにラップして、finallyで閉じます。
所属していません StackOverflow