• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

close sql connection in finally block

 
Dean WinchesterD
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I've been advised to close all sql connections in a finally block but what is the point of using the finally block if you need to catch another exception in it?

let's say we have:



Am I getting this right? What's the point of having a try/catch block if you need to do it all over again in the finally part?

I just don't understand why my original code was 'bad'. I'd rather not ask him right away and get some perspective on this.

Original:


 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very similar to a ReaderBut you should consider whether it is better to keep Connections open and cache them, rather than opening and closing them. I don't know about that latter point, which might be better answered on our databases forum. I shall keep this topic here for the time being, however.
 
Dean WinchesterD
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I've always been told to place the finally block after the catch.

Your reply makes much more sense, thanks alot.

I'll see how it goes from here.


I've talked about them about always opening and closing the connections. It seemed rather ineffecient to me but they did not want me to change it. Might be worth some more research though.

Dean
 
Carey Brown
Ranch Hand
Posts: 1389
16
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except conn.close() can throw an SQLException of its own. That's why Dean had the initial try-catch inside the finally block.

I always use the form Campbell used*. No need to check for null or anything. The only drawback is that if conn.close() throws an SQLException this will discard any SQLException thrown by the rest of the try block, but that's something I'm willing to live with, as I've never had a close() operation on any connection / stream / Reader / Writer throw an exception.

* In fact, I think I taught him this form just a few weeks ago
Well, not completely true: http://www.coderanch.com/t/536851/java/java/finally-close. I just showed him that a null-check is unnecessary.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . I always use the form Campbell used*. . . ..
I was taught a different form, but a few weeks ago when Rob pointed out that form is better.
 
Dean WinchesterD
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Where sqlHelper is an instance of a class with all static sql methods.

Does this look clean/alright?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the general approach which you can use for your helper class, java.sql.Connection, java.io.Reader, java.io.Writer, InputStream, OutputStream, and basically anything you should close after use.
 
Dean WinchesterD
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, this was very helpful.

Dean
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dean WinchesterD wrote:Where sqlHelper is an instance of a class with all static sql methods.

If they are all static method you should just be using the class name to access them, not an instance.
 
Dean WinchesterD
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Dean WinchesterD wrote:Where sqlHelper is an instance of a class with all static sql methods.

If they are all static method you should just be using the class name to access them, not an instance.


My bad, they are not static at all. Only the instance is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic