Spring things rollback mechanism (transaction exception rollback, catch exception not thrown will not be rolled back)

Source: Internet
Author: User
Tags getmessage

Spring does not roll back when an exception is caught in the catch

Why don't you roll??  

Spring AOP Exception Trapping principle: The method being intercepted explicitly throws an exception and cannot be processed so that the AOP agent catches the exception of the method before it can be rolled back, and AOP captures only runtimeexception exceptions by default;

Solution:

1, such as service layer processing transaction, then the method in the service does not do the exception capture, or in the Catch statement last add the throw new RuntimeException () statement, in order to let the AOP catch exception to rollback, And on the service upper level (WebService client, view layer action) to continue catching this exception and handling

Catch (Exception e) {            json.setstatus (statuscode.error);            Json.setmessage (E.getmessage ());            System.out.println ("Add user exception, error message:" +e.getmessage ());             // continue to throw exceptions            Throw New runtimeexception ();}


2. Add in the catch statement of the service layer method: Transactionaspectsupport.currenttransactionstatus (). Setrollbackonly (); statement, manual rollback, So that the upper layer does not have to deal with the exception (now the project practice)

Catch (Exception e) {            json.setstatus (statuscode.error);            Json.setmessage (E.getmessage ());            System.out.println ("Add user exception, error message:" +e.getmessage ());             // Manually Roll back things             transactionaspectsupport.currenttransactionstatus (). Setrollbackonly ();        }

Spring things rollback mechanism (transaction exception rollback, catch exception not thrown will not be rolled back)

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.