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
Resource leak warning in eclipse
提问by Reto Strub
In Eclipse
I received a warning Resource leak: 'ps' is not closed at this location
that I don't understand.
在Eclipse
我收到一个Resource leak: 'ps' is not closed at this location
我不明白的警告。
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:
在我的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 = null
before 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 AutoClosable
yourself. You should initialize those resources in special initialization section of try
statementcommented:
如果您收到此警告,则说明您使用的是 Java 7。在这种情况下,您不应关闭AutoClosable
自己实现的资源。您应该在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.
我确实不知道为什么if/else
语句的存在会导致警告出现或消失。但是 java 7 推荐使用我上面描述的自动关闭资源的方式,所以试试这个。
回答by Alexander Pogrebnyak
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
和use
块。另外,从初始化块中抛出异常(或提前返回)。这样当你在use
block之后释放资源时就不需要检查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 作为变量名是一个奇怪的故障。谢谢查理