Forums Register Login

Two ways to close a connection

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
i think you mean the following:

right?

thank you
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
do you think i need to close the statement too?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1079 times.
Similar Threads
Connecting to a db with a servlet or bean
diferent behaviour for diferent mySql drivers
java.sql.SQLException: Server configuration denies access to data source
Newbie having problems with StrutsMocktestCase
NullPointerException - connecting it mySQL
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:35:32.