• 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

Why do we need to close the ResultSet?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do we need to close the ResultSet or Statement objects explicitly every time we use it?Can any body tell me what will happen if we dont close these
objects explicitly?
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Failure to close these objects makes it more likely that you will exceed the number of available database cursors. For instance, Oracle reports an ORA-01000 error. When this error is propagated to an application server such as WebLogic Server, SQLException is thrown with a message like this:


Also, failure to close a Statement object can cause a database lock to be retained unnecessarily.

Even if objects are closed just after usage, you should ensure closure in the finally block. Note that each closure is done in a separate try/catch block.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger hinted but didn't quite state that ResultSEts and some other things in JDBC are database resources, not Java resources. Java is pretty good at memory management, although it is still possible to create memory (or 'resource') leakage it isn't quite as easy as it once was.

Things change when you mix Java with external applications. If these applications manage a finite number of resources, Java can try to get it right for you but may not be perfect. I believe you can have similar problems in AWT if you don't close some of the resources.

Personally I prefer the defensive technique: Always try to make sure you release resources when required, because if it ever becomes an issue it will take you two weeks or more to diagnose and fix the problem
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what David O'Meara said is exaclty suitable.if you won't close your resultset object or statement,connection objects what exaclty happnes is before serving your requst it(ResultSet) occupies some memory on the heap right?if you won't close the object explicitly it (Object) won't release the resources which it consumes until the garbage collector will come into the picture.
what ever resources you are consumed from heap if you release it explicity it leads efficient memory management.this is one of the reason.
It is strictly applied for Connectionpooling in JDBC.
Thanks,
G Sirish Reddy.,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic