Spring transaction is abnormally rolled back, and if the exception is caught, it will not be rolled back

I recently encountered a situation where the transaction does not roll back. I also consider that there is a bug in the JPA transaction? I think too much....  
  In order to print the log clearly, I add tyr catch to many methods, and print the log in the catch. But here comes the situation. When this method is abnormal, the log is printed, but the added transaction is not rolled back.

  Example:  
   methods like this do not rollback (one method fails, the other does not):  

[html]  view plain  copy
  1. if(userSave){          
  2.     try {         
  3.         userDao.save(user);          
  4.         userCapabilityQuotaDao.save(capabilityQuota);         
  5.      } catch (Exception e) {          
  6.         logger.info("Capability activation interface, account opening exception, exception information: "+e);         
  7.      }         
  8.  }  

The following methods roll back (one method fails, the other method rolls back):

[html]  view plain  copy
  1. if(userSave){         
  2.      try {          
  3.         userDao.save(user);          
  4.         userCapabilityQuotaDao.save(capabilityQuota);         
  5.        } catch (Exception e) {         
  6.         logger.info("Capability activation interface, account opening exception, exception information: "+e);          
  7.         throw new RuntimeException();         
  8.      }          
  9. }  
or:
[html]  view plain  copy
  1. if(userSave){          
  2.     try {          
  3.         userDao.save(user);          
  4.         userCapabilityQuotaDao.save(capabilityQuota);          
  5.     } catch (Exception e) {          
  6.         logger.info("Capability activation interface, account opening exception, exception information: "+e);          
  7.         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();         
  8.     }         
  9.  }  

Why not go away? ? I don't understand Spring's transaction mechanism. !
   By default spring transactions are only rolled back when an uncaught runtimeexcetpion occurs.  
   Spring aop exception capture principle: The intercepted method needs to throw an exception explicitly, and cannot be processed in any way, so that the aop proxy can catch the exception of the method and roll back. By default, aop only captures the exception of runtimeexception, but it can
    
Catch specific exceptions   through   configuration and roll back
  . In other words, do not use try catch in service methods or add throw new runtimeexcetpion() at the end of catch, so that program exceptions can be caught and rolled back by aop
  . Solution: 
  Solution : 1. For example, when the service layer handles transactions, the methods in the service do not do exception capture, or add a throw new RuntimeException() statement at the end of the catch statement, so that aop catches the exception and then rolls back, and in the upper layer of the service (webservice client side, view layer action) to continue to capture this exception and handle the
  solution 2. Add in the catch statement of the service layer method: TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); statement, roll back manually, so that the upper layer does not need to handle the exception ( current project practice)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326846604&siteId=291194637