18

In Eclipse I received a warning Resource leak: 'ps' is not closed at this location that I don't understand.

In my Java code I declare the "ps" as a Prepared Statement and I use (and close) it many times. Then I've the following sequence:

try {
    if(condition) {
        ps = c.prepareStatement("UPDATE 1 ...");
    } else {
        ps = c.prepareStatement("UPDATE 2 ...");
    }
    ps.executeUpdate();
} catch (SQLException e) {
    // exception handling
} finally {
    if (null != ps) 
        try { 
            ps.close(); 
        } catch (SQLException e) { 
            // exception handling
        };
}

The "Resource leak"-Warning comes on the "Update"-Statement in the else section. If I set ps = null before I start the try block, there is no warning.

If the second UPDATE-Statement is commented out, no warning will be shown.

Is that an understanding or a java / eclipse problem?

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
Reto Strub
  • 181
  • 1
  • 1
  • 8
  • 1
    My guess is that Eclipse is detecting that you're using the prepared statement object previously and due to potential for the SQLException may not close it properly. If you do `ps = null;` in the finally block it'll probably be fixed and this would be a more rational place to clean it up. – Philip Whitehouse Feb 13 '13 at 20:58

3 Answers3

11

If you have this warning you are using Java 7. In this case you should not close the resource that implements AutoClosable yourself. You should initialize those resources in special initialization section of try statementcommented:

// decide which update statement you need:
// (your if should be here)
String update = ....;
try (
     ps = c.prepareStatement(update);
) {
   // use prepared statement here.
} catch (SQLException) {
   // log your exception
   throw new RuntimeException(e);
}
// no finally block is needed. The resource will be closed automatically.

I indeed do not know why presence of if/else statement causes the warning to appear or disappear. But java 7 recommends the way to work with auto closable resources that I described above, so try this.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Thanks, this helped me out. Since I'm not yet ready to fully commit to Java 7 I've just told Eclipse to set resource leaks to ignore. Probably dangerous, but will work for now. – rjcarr Feb 27 '13 at 22:02
3

I think, it's a problem with the checker that your are using.

Break your code into initialization and use blocks. Also, throw exception out of the initialization block ( or do an early return ). This way there is no need to check for null when you release the resource after use block

// initialization
// Note that ps is declared final.
// I think it will help to silence your checker
final PreparedStatement ps;

try {
    if( bedingungen ... ) {
        ps = c.prepareStatement("UPDATE 1 ...");
    } else {
        ps = c.prepareStatement("UPDATE 2 ...");
    }
} 
catch (SQLException e) {
    log.error("Problem creating prepared statement, e );
    throw e;
}

// use
try {
    ps.executeUpdate();
} catch (SQLException e) {
    log.error("Problem decrementing palets on " + srcElement.getName() + 
        ": " +    e.getMessage());
}
finally {
    try {
        ps.close();
    } catch (SQLException e) {
        log.warn("Error closing PreparedStatement: " + e.getMessage());
    };
}
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
-5

Change the variable name from c to mC. I think it's a weird glitch while using c as a variable name. Thanks Charlie