The javadoc for Connection#rollback clearly states:
This method should be used only when auto-commit mode has been disabled.
A similar caveat exists for the commit
method.
However, looking at the code, e.g. in Apache commons DbUtils I see only:
public static void rollback(Connection conn) throws SQLException {
if (conn != null) {
conn.rollback();
}
}
It seems weird to have a utility function only to guard against NPE and to omit to test the auto-commit mode. I would have expected something along the following lines:
public static void rollback(Connection conn) throws SQLException {
if (conn != null) {
final boolean autoCommit = (boolean) conn.getAutoCommit();
if (!autoCommit)
DbUtils.rollback(conn);
}
}
Is the caveat in the javadoc (that rollback
should only be called when auto-commit mode has been disabled) actually observed in practice and, if so, why would a successful JDBC library not bother with it?