I have a small doubt in closing connection objects. If i close the connection objects directly without closing the PreparedStatement and ResultSet object in a method. Will the db connection object be closed or will it become a dangling object waiting for GC to run and claim them.
When i tried sometimes i get the connection object left open and sometimes it get closed. Why does it behaves like this..
It's a good practice to always close any database connection you open within a finally block. Leaving connections open is one of the biggest sources of memory leaks in large systems.
Originally posted by Sunny Jain: i heard the if I close the preparedStatment, then result set will close automatically. Does this behavior is guaranteed ?
Nothing is guaranteed. It depends on the implementation of the driver.
That said, most people have a utility method that closes all the resources. What's the harm in closing the ResultSet in that method too? It's only in one place after all.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.