Friday, 15 February 2013

java - Resource leak warning in eclipse -



java - Resource leak warning in eclipse -

in eclipse received warning resource leak: 'ps' not closed @ location don't understand.

in java code declare "ps" prepared statement , utilize (and close) many times. i've next sequence:

try { if(condition) { ps = c.preparestatement("update 1 ..."); } else { ps = c.preparestatement("update 2 ..."); } ps.executeupdate(); } grab (sqlexception e) { // exception handling } { if (null != ps) seek { ps.close(); } grab (sqlexception e) { // exception handling }; }

the "resource leak"-warning comes on "update"-statement in else section. if set ps = null before start seek block, there no warning.

if sec update-statement commented out, no warning shown.

is understanding or java / eclipse problem?

if have warning using java 7. in case should not close resource implements autoclosable yourself. should initialize resources in special initialization section of try statementcommented:

// decide update statement need: // (your if should here) string update = ....; seek ( ps = c.preparestatement(update); ) { // utilize prepared statement here. } grab (sqlexception) { // log exception throw new runtimeexception(e); } // no block needed. resource closed automatically.

i indeed not know why presence of if/else statement causes warning appear or disappear. java 7 recommends way work auto closable resources described above, seek this.

java eclipse resource-leak

No comments:

Post a Comment