I have actived in Eclipse that it shows possible database connection leaks.
I have this piece of code:
public static Administrator getAdministrator(String gebruikersnaam, String wachtwoord) throws CustomException {
Connection connectie = null;
PreparedStatement prepStmt = null;
ResultSet result = null;
try {
connectie = ConnectionPool.getInstance().getConnection(true, true);
prepStmt = connectie.prepareStatement(QUERY_GET_ADMINISTRATOR);
prepStmt.setString(1, gebruikersnaam);
prepStmt.setString(2, wachtwoord);
result = prepStmt.executeQuery();
if (result.next()) {
Administrator admin = new Administrator(result.getString(1), result.getString(2), result.getString(3));
return admin;
} else {
return null;
}
} catch (SQLException e) {
throw new CustomException("Fout opgetreden bij het opvragen van een administrator uit de databank (" + e.getMessage() + ").");
} finally {
close(result);
close(prepStmt);
close(connectie);
}
}
Eclipse is warning at the line with the returns and and at the line where I throw the CustomException that there is a possible resource leak in that the ResultSet result is not closed. However I have written to close the ResultSet, the PreparedStatement and the Connection objects in my finally clause. Is this correctly written or are there better and cleaner ways to code this?
Here a screenshot of the warnings:
This looks good. The IDE can only know so much. The finally
will get called.
However you may want to consider what affect the other warnings are having? As they are obscured by the warning box, I can not tell.