Recently, I encountered a situation where the transaction does not roll back. I also considered saying that there are bugs in JPA's transactions? I'm thinking too much...
In order to print the logs clearly, I add tyrcatch to print the logs in the catch. But the situation here comes. When this method is abnormal, the log is printed, but the added transaction is not rolled back.
example:
Methods like this won't rollback (one method errors, the other method won't rollback):
if(userSave){ try { userDao.save(user); userCapabilityQuotaDao.save(capabilityQuota); } catch (Exception e) { logger.info("Capability to open the interface, account opening exception, exception information: "+e); } }The following method rolls back (one method errors, the other method will roll back):
if(userSave){ try { userDao.save(user); userCapabilityQuotaDao.save(capabilityQuota); } catch (Exception e) { logger.info("Capability to open the interface, account opening exception, exception information: "+e); throw new RuntimeException(); } }or:
if(userSave){ try { userDao.save(user); userCapabilityQuotaDao.save(capabilityQuota); } catch (Exception e) { logger.info("Capability to open the interface, account opening exception, exception information: "+e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } }Why can't you get out? ? It is not understanding the transaction mechanism of spring. ! !
*****The default spring transaction is only rolled back when an uncaught runtimeexcetpion occurs. *******
The principle of springaop exception capture: The intercepted method needs to throw an exception explicitly and cannot be processed in any way. In this way, the aop agent can catch the exception of the method and roll back. By default, aop only catches the exception of the runtimeexception, but can pass.
Configure to catch specific exceptions and roll back
In other words, do not use trycatch in the service method or add thrownewruntimeexcetpion() to the catch, so that the program can be caught by aop and rolled back when the program is exceptional.
Solution:
Solution 1. For example, if the service layer handles transactions, then the method in the service does not perform exception capture, or add a thrownewRuntimeException() statement to the catch statement, so that aop catches the exception and then rolls back, and the service upper layer (webservice client, view layer action) must continue to catch and handle the exception.
Solution 2. Add the: TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); statement to the catch statement of the service layer method, manually rollback, so that the upper layer does not need to handle exceptions (the current project practice)
Summarize
The above is all the content of this article about the analysis of spring transaction exception rollback instances. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!