You must first execute the Con.setautocommit (false) method to set the JDBC transaction to manual commit, otherwise the manual commit Con.commit () is not valid, and the manual rollback Con.rollback () raises the SqlException: The rollback operation cannot be called when the autocommit mode is set to "true".
We execute the Con.setautocommit (False) method, set the JDBC transaction to manual commit, and then discuss the JDBC transaction and the database transaction in 9 scenarios.
Description: A stored procedure has an INSERT statement under database Transaction management, and another INSERT statement under JDBC Transaction management, assuming that the statement we are executing now does not violate the constraint.
Scenario One: Both the JDBC transaction and the database transaction are neither committed nor rolled back
Result: No exception occurred, but the statement was not committed.
Scenario Two: The JDBC transaction neither commits nor rolls back, the database transaction commits
Result: No exception occurred, but none of the two statements were committed.
Scenario Three: The JDBC transaction is neither committed nor rolled back, and the database transaction is rolled back
Result: No exception occurred, but none of the two statements were committed.
Scenario Four: Both the JDBC transaction and the database transaction are rolled back
Result: SqlException occurred while executing con.rollback (): The server was unable to proceed with the transaction, description: 3600000001. Of course, none of the two statements were successful.
Scenario Five: JDBC transaction rollback, database transaction commit
Result: Although the database transaction was committed, both statements were unsuccessful, and the rollback of the JDBC transaction affected the database transaction.
Scenario Six: JDBC transaction rollback, database transaction neither committed nor rolled back
Result: No exception occurred, but none of the two statements were committed.
Scenario Seven: JDBC transaction commit, database transaction rollback
Result: SqlException occurred while executing con.rollback (): The server was unable to proceed with the transaction, description: 3600000001. Of course, none of the two statements were successful.
Scenario Eight: Both the JDBC transaction and the database transaction are committed
Result: Everything goes well and two statements are executed correctly.
Scenario Nine: JDBC Transaction commits, database transactions are neither committed nor rolled back
Result: No exception occurred, but none of the two statements were committed.
Summary: Only one--JDBC transaction and database transaction are committed when a successful commit occurs, Con.commit () fails if the database transaction commits or rolls back, and Con.rollback () reports an exception.
Recommended configuration: If both the JDBC transaction and the database transaction are present, the database transaction is complete with the commit and rollback, and the JDBC transaction simply executes the Con.commit () method, which automatically determines whether the commit is true based on the database transaction.
Discussion of nesting of JDBC transaction and database transactions.