service calls different dao without rollback

1. If the exception is caught in the service method, the transaction is the 
       default spring transaction that will not be rolled back. It will only be 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 the service method or add throw new runtimeexcetpion() at the end of catch, so that program exceptions can be caught by aop and rolled back 
  the solution:  
  Option 1. For example, the service layer handles transactions, then 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 can catch the exception and then roll back, and continue at the controller layer. Catch 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 (the current project practice)

 

Taken from: http://itindex.net/detail/54929-service-exception

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324809150&siteId=291194637