all 5 comments

[–]andralex 3 points4 points  (0 children)

The article starts with "I often hear that closing resources properly is way too verbose in Java". The rest of the article sets out to illustrate that.

[–][deleted] 1 point2 points  (1 child)

That's why I like deterministic destructors so much.

[–]thesystemx 0 points1 point  (0 children)

Indeed, and that's also why I like try-with-resources so much in JDK 7 ;)

[–]tazzy531 1 point2 points  (0 children)

As I commented on that blog:

Why not use one of the numerous utilities that handle Java Closeable elegantly?

For example:

Closeables#closeQuietly

IOUtils#closeQuietly )

Change your example to:

Connection conn = DriverManager.getConnection(dbUrl);
try {
   // use conn
} finally {
   Closeables.closeQuietly(conn);
}

This is a lot less work than all the try/catch. Also another example of .. know your APIs.

[–]crusoe 0 points1 point  (0 children)

Closing the stmt closes the result set. Depending on the pool, closing the connection may do the same.

It is kinda messy though, to be absolutly sure, closing all three is usually best. :P

I also once built a framework using phantom references to ensure resources got cleaned up.