• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

close()

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know one thing.

What happens if i dont close Connections Statements and Resultsets?

I think garbage collectors will clean them even they are not closed.Because they wont be used and not referenced to anywhere and anymore.

I wonder whats going on the database side?
[ July 04, 2005: Message edited by: rasit fidanov ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would guess that most decent JDBC drivers would, indeed, have the Connections and othe objects close() themselves in finalize(). But you can't depend on garbage collection to clean up after you, because you don't know when it will happen. All operating systems place limits on the number of simultaneous network connections you can have open, and on the numbers of other system resources you can consume. You could easily open enough Connections, for example, to exhaust all the available network sockets (i.e., database connections) without filling up memory -- so you'd not be able to connect, but the garbage collector wouldn't fire, so none of the old Connections would be closed.
 
rasit fidanov
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the answer..

in java side garbage collectors will collect non-referenced items.
I wonder whats going on the database?

I think on db side somethink just like "session" will have opened for every connections to db.

Are they destroyed when connections collected by garbage collector on jdbc side?
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, we'll have to regard Oracle 9i JDBC drivers as not decent! They do not have finalizer methods; instead, they perform cleanup routines by using the close() method of the ResultSet and Statement classes.

This is my understanding of what happens. A ResultSet represents a database cursor which indexes a particular row and knows how to get the next one. What is this cursor? It's an area of memory in the database in which the executed SQL string is parsed and the result held. (I'm not sure whether all the data is necessarily held here or maybe pointers to where the data is.) When you close a ResultSet or Statement, this releases the corresponding cursor in the database. So, failure to close can cause two problems: you can suffer serious memory leaks and run out of cursors in the database.

As for Connections: you need to close them or you will probably exhaust the connection pool. If you are not using connection pooling, then closing the Connection will (probably) cause the JDBC driver to close any open Statement objects associated with it, thus releasing the cursor objects on the server side.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic