Spring transaction exception rollback, catch exception is not thrown and will not be rolled back

Source: Internet
Author: User
Tags try catch

I have recently encountered a transaction not rollback, I also consider that the JPA transaction has a bug? I think more ...
In order to print clear logs, many methods I add Tyr catch to print the log in the catch. But the situation here, when this method is abnormal when the log is printed, but the addition of the transaction has not been rolled back.

Example:
A method like this does not roll back (one method fails and the other does not):

[HTML]View PlainCopy 
    1. if (usersave) {           
    2.     try {         
    3.         userdao.save (user);           
    4.          Usercapabilityquotadao.save (Capabilityquota);          
    5.      } catch  (exception e)  {           
    6.         logger.info (" Ability to open Interface, account opening exception, exception information: "+e";          
    7.       }         
    8.  }  


The following method is rolled back (one method error and another method is rolled back):

[HTML]View PlainCopy 
  1. if (Usersave) {
  2. try {
  3. Userdao.save (user);
  4. Usercapabilityquotadao.save (Capabilityquota);
  5. } catch (Exception e) {
  6. Logger.info ("Ability to open Interface, account opening exception, abnormal information:" +e);
  7. throw new RuntimeException ();
  8. }
  9. }

Or:

[HTML]View PlainCopy 
  1. if (Usersave) {
  2. try {
  3. Userdao.save (user);
  4. Usercapabilityquotadao.save (Capabilityquota);
  5. } catch (Exception e) {
  6. Logger.info ("Ability to open Interface, account opening exception, abnormal information:" +e);
  7. Transactionaspectsupport.currenttransactionstatus (). Setrollbackonly ();
  8. }
  9. }

Why don't you roll?? It is not clear to spring's transaction mechanism.!!
The default spring transaction is rolled back only when an uncaught runtimeexcetpion occurs.
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 catches only runtimeexception exceptions by default, but can be

Configuration to catch a specific exception and roll back
In other words, no try catch is used in the service's method or a throw new Runtimeexcetpion () is finally added to the catch, so that the program can be caught by AOP and then rolled back.
Solution:
Scenario 1. For example, the service layer processes the transaction, then no exception is caught in the methods in the service, or the throw new RuntimeException () statement is finally added to the catch statement so that AOP catches the exception and then rolls back. And on the service upper level (WebService client, view layer action) to continue catching this exception and handling
Scenario 2. Add: Transactionaspectsupport.currenttransactionstatus (). Setrollbackonly () in the catch statement of the service layer method; statement, manual rollback, So that the upper layer does not have to deal with the exception (now the project practice)

Spring transaction exception rollback, catch exception is not thrown and 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.