Resource leak warning in eclipse

北城以北 提交于 2019-12-17 18:52:01

问题


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?


回答1:


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.




回答2:


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());
    };
}



回答3:


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



来源:https://stackoverflow.com/questions/14862853/resource-leak-warning-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!