1

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?

Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
  • 3
    The reasoning is, that if you haven't disabled autoCommit, why are you bothering calling `DbUtils.rollback(..)` or `DbUtils.commit(..)` in your code, and if you call them that is clearly a bug that you should notice. To be honest given modern try-with-resources, I find a lot of the `DbUtils` methods pretty useless. – Mark Rotteveel Jun 01 '17 at 14:50

1 Answers1

-1

When auto-comit is enabled the rollback method simply hasno effect.

AlexS
  • 519
  • 5
  • 11