• Post Reply Bookmark Topic Watch Topic
  • New Topic

Closing Connection Object  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I have written a piece of Code which opens a Connection,Creates a PreparedStatment and Excutes a Query.Now i want to close the Stmts,Conns etc

Can anyone suggest which is the best way to do so.A kind of Best Practice.
For Example: 2 Ways which i have seen are

1st
-----------------------
} finally {
try {
if (objlResultSet != null) {
objlResultSet.close();
}

if (objlPreparedStatement != null) {
objlPreparedStatement.close();
}

if (objlConnection != null) {
DbUtils.closeConnection(objlConnection);
}
} catch (Exception objaException) {
error("EXCEPTION",objaException)
}
}

2nd
-----
} finally {
try {
if (objlResultSet != null) {
objlResultSet.close();
}
} catch(Exception objaException){
error("EXCEPTION");
}

try{
if (objlPreparedStatement != null) {
objlPreparedStatement.close();
}
}catch(Exception objaException){
error("EXCEPTION");
}
try{
if (objlConnection != null) {
DbUtils.closeConnection(objlConnection);
}
} catch (Exception objaException) {
error("EXCEPTION);
}

Can anyone suggest which one is better and why.Incase there is a further efficient methodology to do so .Please suggest

-Thanks-
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to explicitly dispose of anything other than the connection - all the other JDBC resources are tied to it. And the only thing you really have to remember about that is to close it in a finally block - to make sure the connection is freed up regardless of what happens in your try block.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!