eclipse eclipse中的资源泄漏警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14862853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:00:06  来源:igfitidea点击:

Resource leak warning in eclipse

javaeclipseresource-leak

提问by Reto Strub

In EclipseI received a warning Resource leak: 'ps' is not closed at this locationthat I don't understand.

Eclipse我收到一个Resource leak: 'ps' is not closed at this location我不明白的警告。

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

在我的Java代码中,我将“ps”声明为准备好的语句,并且多次使用(并关闭)它。然后我有以下顺序:

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 = nullbefore I start the try block, there is no warning.

“资源泄漏”-警告出现在 else 部分的“更新”-语句中。如果我ps = null在开始 try 块之前设置,则没有警告。

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

如果第二个 UPDATE-Statement 被注释掉,则不会显示警告。

Is that an understanding or a java / eclipse problem?

这是理解还是java/eclipse问题?

回答by AlexR

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

如果您收到此警告,则说明您使用的是 Java 7。在这种情况下,您不应关闭AutoClosable自己实现的资源。您应该在trystatementcommented 的特殊初始化部分初始化这些资源:

// 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/elsestatement 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.

我确实不知道为什么if/else语句的存在会导致警告出现或消失。但是 java 7 推荐使用我上面描述的自动关闭资源的方式,所以试试这个。

回答by Alexander Pogrebnyak

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

我认为,这是您使用的检查器的问题。

Break your code into initializationand useblocks. 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 useblock

将您的代码分解为initializationuse块。另外,从初始化块中抛出异常(或提前返回)。这样当你在useblock之后释放资源时就不需要检查null

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

回答by user2098333

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

将变量名称从 c 更改为 mC。我认为使用 c 作为变量名是一个奇怪的故障。谢谢查理