Title : Java - SonarLint Detect the Blocker - Close this "PreparedStatement" in a "finally" clause.
Question : I have multiple PreparedStatement in one method (example), then I have done close the PreparedStatement but the first line ( // prepareStatement no 1 - table_a) of PreparedStatement still detect Blocker - ( Close this "PreparedStatement" in a "finally" clause) :
PreparedStatement preparedStatement = null;
try {
connection.setAutoCommit(false);
// prepareStatement no 1
preparedStatement = connection.prepareStatement("delete from table_a where abc_id=?");
preparedStatement.setString(1, abc_id);
preparedStatement.executeUpdate();
// prepareStatement no 2
preparedStatement = connection.prepareStatement("delete from table_b where bc_id=?");
preparedStatement.setString(1, bc_id);
preparedStatement.executeUpdate();
// prepareStatement no 3
preparedStatement = connection.prepareStatement("delete from table_c where cd_id=?");
preparedStatement.setString(1, cd_id);
preparedStatement.executeUpdate();
// prepareStatement no 4
preparedStatement = connection.prepareStatement("delete from table_d where de_id=?");
preparedStatement.setString(1, de_id);
preparedStatement.executeUpdate();
// prepareStatement no 5
preparedStatement = connection.prepareStatement("delete from table_e where ef_id=?");
preparedStatement.setString(1, ef_id);
preparedStatement.executeUpdate();
// prepareStatement no 6
preparedStatement = connection.prepareStatement("delete from table_f where fg_id=?");
preparedStatement.setString(1, fg_id);
preparedStatement.executeUpdate();
connection.commit();
} catch(SQLException e) {
log.error(e);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
connection.setAutoCommit(true);
} catch (SQLException e) {
log.error(e);
}
}