File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes statement and resultset closing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "statement and resultset closing" Watch "statement and resultset closing" New topic

statement and resultset closing

shashank beerla

Joined: Jun 07, 2013
Posts: 27
is this right way to do it ?

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11952

shashank beerla wrote:is this right way to do it ?

Do what?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
shashank beerla

Joined: Jun 07, 2013
Posts: 27
closing the resultset and statement
shashank beerla

Joined: Jun 07, 2013
Posts: 27
as you can see i am using single statement for multiple queries
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

That's a pretty good attempt. The only problem is, the close() method can also throw an SQLException. At least it's declared that way. And yes, you're catching that exception. However if that exception were actually thrown, let's say at line 54 for example, then the PoolManager.returnConnection line would not be executed. So you aren't dealing with it adequately, if you want to be 100% bullet-proof. Even if line 54 threw an exception, you would want to carry on with the rest of the code in the finally-block.

And by the way setting a method's local variable to null is usually pointless, and when you do it immediately before leaving the method, it's always pointless.

I agree. Here's the link:
subject: statement and resultset closing
It's not a secret anymore!