Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Does a SQLException close a Connection?

843859Nov 5 2009 — edited Nov 20 2014
I've been using the following pattern for database access for quite some time now:

try {
PreparedStatement stmt = conn.prepareStatement(query);
stmt.set ...
stmt.executeUpdate();
stmt.close();
conn.close();
} catch (SQLException ex) {
logger.log(ex);
}

But now I need to attempt an INSERT and if fails due to a DUPLICATE KEY, then try again. So in others words, I need to use recursion.

I've got the error detection and recursion logic all squared away but I am worried about side effects, especially about Connections.

So my question is: when a Statement throws a SQLException, is the Connection implicitly closed or is it kept alive?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 3 2009
Added on Nov 5 2009
11 comments
367 views