Reusing a prepared state

I ran findbugs on our code base, and he pointed out that there are two more statements that still need to be closed. In this section of code, we run:

preparedStatement = connection.prepareStatement(query); 

for three different queries, reusing the prepared state. In the finally block, we close the resource:

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

If the statement should be closed before the next connection .prepareStatement (query); or is it a suspicious search?

+3
java prepared-statement findbugs
source share
1 answer

Yes, the statement must be closed before making the next .prepareStatement connection. Otherwise, you lose your link to the unclosed previous (in other words, leaks). Wrap the attempt {} finally {} around using each statement, closing it at the end.

+8
source share

All Articles