Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two ways to close a connection

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you mean the following:

right?

thank you
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you think i need to close the statement too?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic