Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#681 Method may fail to close database resource, generates noice.

open-postponed
William Pugh
2
2015-02-15
2008-12-03
Samuel Ĺslund
No

The below function triggers the
"Method may fail to close database resource" message.

Note the returned ResultSet.

At least in java 1.4 closing the Statement will Close the Resultset also. The Statement is reachable from the Resultset so it is not lost.

I was _so_ happy when I first saw the message, and only three of them. Getting control of where my predecessor have left these resultsets and statements open is a major pain.

public ResultSet query\(String q\) throws SQLException
\{

    //if\(q.indexOf\("00:08:1e:00:02:56"\) \!= -1\)
    //  System.out.println \(q + "\n---"\);
    //System.out.println \(q\);
    if\(DEBUG\_PRINT\_QUERIES == true\) \{
        System.out.println \("QUERY: " + q\);
    \}

    ResultSet rs;
    try \{
        Statement stmt = connection.createStatement\(\);
        rs = stmt.executeQuery\(q\);
        // stmt.close\(\); // Detta stänger resultsetet, ingen bra idé alltså.
        return rs;
    \}
    catch\(SQLException e\) \{

        WilmaDB.logWarning\(this.getClass\(\).getName\(\), "query\(\)", "Exception on '"+q+"'." + "\nMessage: " + e.getMessage\(\)\);
        reconnect\(\);
        Statement stmt = connection.createStatement\(\);
        rs = stmt.executeQuery\(q\);
        // stmt.close\(\); // Detta stänger resultsetet, ingen bra idé alltså.
        return rs;          
    \}
\}

Discussion

  • Mark Daly
    Mark Daly
    2008-12-13

    • assigned_to: nobody --> wpugh
     
  • Mark Daly
    Mark Daly
    2008-12-13

    Repro/test case committed, reassigning for further investigation.

    Note that, while this issue is related to the same classes as those involved in bug 2207693, the two bugs are quite different.
    In 2207693, a warning is thrown because the result set created by a statement is not closed; the warning is incorrect because the result set will be automatically closed when the statement that produced it is closed.
    In this bug, a warning is thrown because the statement used to produce a result set (which is subsequently returned) is not closed. However, because the result set can refer to the statement from which it originated, the statement does not necessarily need to be closed within the test class.

     
  • William Pugh
    William Pugh
    2008-12-20

    • priority: 5 --> 4
     
  • William Pugh
    William Pugh
    2009-02-11

    • priority: 4 --> 2
     
  • William Pugh
    William Pugh
    2011-12-21

    Fixed for OBL_UNSATISFIED_OBLIGATION; now correctly reported as OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE.

    Still reporting ODR_OPEN_DATABASE_RESOURCE

     
  • William Pugh
    William Pugh
    2011-12-21

    • status: open --> open-postponed