Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Closing DBObjects

 
Jotty Tharakan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you close the connection whether all related DBObjects like Resultset,statement objects are also closed or we need to close each and every one of them explicitly.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is good practice to close the object as soon as you are finished with it to free up the database resource as soon as possible.
Jamie
 
Jotty Tharakan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamie ,
I'm agreeing with u but is it necessary to close all objects like resultset and statement objects or closing the connection itself will close all related objects ?
Can u pls clear this point
 
Shashank Hiwarkar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jotty,
I aggree to Jamie, We should free the database resources as soon as possible.
But look how we declare statements amd resultset
Statement stmt=connection.createStatement();
ResultSet rs=stmt.getResultSet();
that means statement and resultset are created with the help of connection object.
When you are saying "connection.close()"
you don't have to explicitly close Statements and ResultSet. The resources will be freed at the same time.
If anybody has some other views please let me know.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the connection object will invoke the methods Statement.close() and ResultSet.close() on all associated statements/resultsets when you invoke the method Connection.close(). But why would you not release the DB resources at the earliest possible time? By not doing so, it will make your code more less readable and definitely deplete network/DB performance on higher traffic applications. Some DB's are affected more than others when it comes to increased traffic.
Jamie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic