aspose file tools*
The moose likes JDBC and the fly likes Two ways to close a connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Two ways to close a connection" Watch "Two ways to close a connection" New topic
Author

Two ways to close a connection

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Hi
i'm aware of new and old way of closing connections:
the old way (here i'm unsure if i really get it closed and if i need to catch something...) please note i'm saying this because sometimes i want to manually delete WEB-INF dir and get an error message saying mysql driver is in use, when i have everything closed and terminated!

and here is the try with resources way: is it correct?



thanks in advance

java amateur
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

You're not closing the connection in the "try with resources" version at all.

The Connection interface is AutoCloseable, so it is possible to close a connection using the new "try with resources" syntax, and it would be the preferred way. But you aren't doing so. You would need to move the Connection declaration into the try statement.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i think you mean the following:

right?

thank you
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Not exactly. This way you're closing the connection, but not the statement.

It should look like this:



I'm not 100% sure about the syntax -- I'm not on JDK7 yet and cannot test it out.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
do you think i need to close the statement too?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

miguel lisboa wrote:do you think i need to close the statement too?

I had thought you'd ask...

Short answer: yes.

Long answer: it is true that it is probably not strictly necessary with most JDBC drivers; if a connection is closed, the statement's resources will be - in most cases - freed too. But you cannot be really sure about this unless it is explicitly stated in the documentation of the JDBC driver you use. Simply closing the statement is easier than verifying you don't need to close it. And double so when using the try-with-resources syntax, where you don't have to call it explicitly, just move the declaration to the try () section.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm using mysql, so i believe i'm quite safe without closing the statement; anyway thanks for the advice and explanation

as to my very first code, is it ok or is there something to catch?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

miguel lisboa wrote:as to my very first code, is it ok or is there something to catch?

You're passing the SQLException up to the caller, somewhere you'll have to handle it eventually.

I personally don't allow SQLExceptions to escape the DAO layer, unless I can handle them somehow, I wrap them into an unchecked exception and have the topmost layer of code catch them and log them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two ways to close a connection