Spring transaction management unexpectedrollbackexception

Source: Internet
Author: User

When using Spring transaction management, the program reports the following:

org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only  

The code of the program is:

Servicea. methioda {beforedone; methodb (); try {serviceb. methodb {}; // an exception is marked as rollback, and dosetrollbackonly (Status);} catch {// when an exception is caught and transferred to commit, it has been marked as rollback, roll back and throw a new exception} Other done ;}


// Propagation: propagation is required
I didn't understand the Spring transaction mechanism.
When this method is called and executed, two points are represented by Spring transaction proxy. Servicea. methoda (), serviceb. methodb (), any exception event in the two methods will be rolled back.
(In fact, servicea # methoda is marked with exceptions and rolled back directly due to the nested transactions. serviceb # methodb only marks the rollback status and rolls back when the service is called back to servicea # methoda ). In this method, serviceb # A methodb exception transaction is marked as rollback and continues to be executed to servicea. methoda (), but it is captured by methoda, so it will not be rolled back and will be executed until the final commit. During commit, spring determines the rollback flag. If a rollback flag exists, it rolls back and throws a unexpectedrollbackexception.

Solution:
The method in servicea # methoda is not affected by serviceb # methodb. It seems that both methods are not very good. 1. serviceb. methodb is not declared as a transaction proxy.
In many cases, serviceb. methodb is also used in other places to manage transactions. At this time, you can override a method.
But pay attention to the data consistency in this method.
2. Like 1, A serviceb. methodbne is also rewritten, which does not throw an exception. Pay attention to the data consistency in this method. It feels the same.


I personally feel that I am trying to avoid capturing exceptions to ensure transaction consistency.
(For example, an exception may be thrown due to insufficient conditions when an item is rewarded to everyone, so that the following items are not affected)
A package can be used independently to perform operations on multiple users. Instead of writing two identical methods in the same service,
Cause confusion.
If it is not captured, the system will not roll back to commit when serviceb. methodb () is called.

From this error, we can get:
1. Find out which methods are used by Spring transaction proxy, and all these methods will be rolled back when there is an exception.
(This. methodb call, caused by dynamic proxy)

2. if an exception is thrown inside the proxy method, the transaction is marked as rollback and called at the outermost layer.
If a nested transaction is a new required_new transaction, it will be rolled back directly when that method is called.
For example, in servicea # methoda, roll back directly. In serviceb # methodb, only mark

3. In the transaction commit servicea. methoda (outermost layer)
Commit (transactionstatus) checks the rollbackonly status before committing a transaction,
If this status is set in methodb, roll back the transaction.
If the rollbackonly status is not set, the transaction is committed normally.

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.