sqlite - Java resource leak in PreparedStatement -
sqlite - Java resource leak in PreparedStatement -
i'm writing code in java involves databases (sqlite), , i'm new @ it, need help one. code looks this:
connection connection = null; seek { connection = sqliteconnector.getconnection(database name); preparedstatement prepstat = connection.preparestatement("some sql stuff"); resultset result= prepstat.executequery(); // utilize of result if (condition1){ prepstat = connection.preparestatement("some sql stuff"); result = prepstat.executequery(); // utilize of result if (condition2){ prepstat = connection.preparestatement("some sql stuff"); } else{ prepstat = connection.preparestatement("some sql stuff"); } result = prepstat.executequery(); // utilize of result result.close(); prepstat.close(); } } grab (exception e) { // exception management } finally{ sqliteconnector.closeconnection(connection); }
and exception saying: "resource leak: 'stat' not closed @ location" in else block. how should refactor it?
thank in advance
preparedstatements should explicitly closed in jdbc. general pattern using preparedstatement is:
create statement. set parameters , execute it. set different parameters , execute it. set yet more parameters , execute it. close statement.from java 7 onwards easiest way ensure utilize statement within try resources block.
java sqlite memory-leaks prepared-statement
Comments
Post a Comment