根据
JAVA documentation,Connection#commit()可以抛出SQLException.我的问题是在这种情况下是否仍然会发出回滚.
例如:
Connection con = null;
try {
// assume this method returns an opened connection with setAutoCommit(false)
con = createConnection();
// do DB stuff
con.commit();
} catch (SQLException e) {
if (con != null) {
// what if con.commit() failed, is this still necessary,
// will it hurt anything?
con.rollback();
}
} finally {
if (con != null) {
con.close();
}
}
我实际上将con.rollback()调用到另一种方法中,忽略它抛出的任何异常,所以我觉得我可以在这里.我只是想知道这是否是处理事情的最佳方式.