Does a SQLException close a Connection?
843859Nov 5 2009 — edited Nov 20 2014I'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?