Forums Register Login

Closing Connection Object

+Pie Number of slices to send: Send
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-
+Pie Number of slices to send: Send
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.
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 437 times.
Similar Threads
finally block is not executing
Database best practice
finally block in Exceptions
Connection from DataSource only allows "selects"!
AutoCommit(False) and rollback bst practise
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:26:45.