Discussion of nesting of JDBC transaction and database transactions.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.