Question

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:

enter image description here

Was it helpful?

Solution

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.

OTHER TIPS

What you are doing is okay. Eclipse is just showing a potential warning, but in this case it is okay.

The finally block will always eventually be called (unless the JVM explodes or you do something like System.Exit()).

Somewhat interestingly, depending on how 'close()' is written, you might end up not closing prepStmt or connectie, because if 'result' is null, calling close on it will throw a NullPointerException and then the subsequent closes won't happen.

I appreciate that the above isn't a direct answer to your question.

What may be relevant is that if the IDE is looking for a 'result.close()' then a close(result) might not trigger the warning. Not being a big eclipse user, I can't say.

Best practices moved on since this question was answered.

In case anyone else lands here, bear in mind that the recommended approach is now to use a try-with-resources for each of the Connection, PreparedStatement and ResultSet 'resources'. This approach takes advantage of the AutoCloseable interface and allows this type of finally block to be eliminated.

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top