jQuery in Action, 2nd edition
The moose likes JDBC and the fly likes Open Connection Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Open Connection Objects" Watch "Open Connection Objects" New topic
Author

Open Connection Objects

Puja Agarwal
Greenhorn

Joined: May 29, 2008
Posts: 11
Hi All,

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..


hope i make sense


Thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26184
    
  66

Puja,
It will usually be closed, but it depends on driver implementation. It is good practice to close all resources yourself though.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3652

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.


My Blog: Down Home Country Coding with Scott Selikoff
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

i have one small query :

Suppose i have the following hierarchy of declaration:

Connection Object
PreparedStatement
ResultSet

and following sequence of close :
PreparedStatement.close()
Connection close

i am not closing the resultSet, Can this cause memory leakage in JAVA?

i heard the if I close the preparedStatment, then result set will close automatically. Does this behavior is guaranteed ?


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26184
    
  66

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.
 
subject: Open Connection Objects
 
Similar Threads
method with SQL parameter
Error while indexing a post: java.sql.SQLException: The statement is closed.
java.sql.SQLException: Io exception: Software caused connection abort: recv fail
java.sql.SQLException: Io exception: Software caused connection abort: recv fail
Query about Connection Object