1, in the service method if the exception is captured, the transaction will not be rolled back
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. However, a specific exception can be captured and rolled back by configuration, 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 is not 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 in the controller layer 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)
Try ... in service ... Catch, thrown runtimeexception in the catch block, cannot be rolled back and does not know why.
Now the solution is scenario 2. or controller rollback.
How a transaction cannot be rolled back in service (GO)