Joanne
Originally posted by Tom Griffith:
Hello. If anybody has a minute, this is roughly what is giving me a problem...
//OUTER TRY BLOCK
try{
PreparedStatement statement=null;
} catch (Exception e) {
statement.close();
} finally {
statement.close();
}
In case of error on the try, I want to close 'statement' because there would be the potential for jvm memory problems, but the catch and finally cannot recognize the variable 'statement'. I always thought any variable declared within try would be within scope of catch and finally but i guess thats not the case.
On the other hand, I need to initialize statement within the try because if I move the initialization outside the outer try, i get 'variable may not be initialized' error on the close() statements in the outer catch and finally. Catch-22.
Does anybody have any ideas where i can ensure 'statement' can be closed from the outer catch or finally? Thank you very much.
[ July 28, 2008: Message edited by: Tom Griffith ]
Steve
Joanne
so it should be fine to call it more than once and probably okay to call it even if nothing needs to be released, although we probably need someone who knows more about JDBC than I do to confirm that.Calling the method close on a Statement object that is already closed has no effect.
Joanne
Originally posted by Tom Griffith:
i think i'm just going to reuse the 'statement' variable and only set it to null at the very beginning during initialization..then call remove() on 'statement' after each sql op and in the finally. This way the old objects 'statement' pointed to should be garbage collected and the statement.remove() won't hurt anything and should close any lingering preparedstatements as intended. the key, i think, is to set 'statement' to null just once, during initialization, and avoid setting 'statement' to null anywhere else (just rely on reassigning 'statement' to each new preparedstatement). Thank you again for everything.
[ July 28, 2008: Message edited by: Tom Griffith ]
Steve
i think i'm just going to reuse the 'statement' variable and only set it to null at the very beginning during initialization..then call remove() on 'statement' after each sql op and in the finally.
Steve
Consider Paul's rocket mass heater. |