spring transaction exception rollback instance resolution

  • 2020-12-13 18:57:37
  • OfStack

Recently, I had a transaction that did not roll back, and I also considered saying that JPA's transactions have bug? I've been thinking too much...

In order to print the log clearly, I add tyrcatch to many methods, and print the log in catch. But here comes the situation where the log is printed when this method is abnormal, but the added transaction is not rolled back.

Ex. :

Methods like this do not roll back (one method fails and the other does not roll back) :


if(userSave){     
  try {     
    userDao.save(user);     
    userCapabilityQuotaDao.save(capabilityQuota);     
   } catch (Exception e) {     
    logger.info(" Ability to open interface, account opening exception, exception information: "+e);     
   }     
 } 

The following method is rolled back (one method fails and the other method rolls back) :


if(userSave){     
   try {     
    userDao.save(user);     
    userCapabilityQuotaDao.save(capabilityQuota);     
    } catch (Exception e) {     
    logger.info(" Ability to open 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(" Ability to open interface, account opening exception, exception information: "+e);     
    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();     
  }     
 } 

Why can't you roll? The transaction mechanism of spring is not understood. !!!!!

***** Default spring transactions are rolled back only if uncaptured runtimeexcetpion occurs. * * * * * *

Principle of springaop exception capture: The method being intercepted needs to explicitly throw an exception without any processing, so that the aop agent can catch the exception of the method and roll back. By default, aop only catches the exception of runtimeexception, but it can pass.

Is configured to catch a specific exception and roll back

In other words, do not use trycatch in service's methods or add thrownewruntimeexcetpion () at the end of catch so that program exceptions can be caught by aop and rolled back

Solutions:

Scenario 1. For example, the service layer handles transactions, then the method in service does not catch exceptions, or the thrownewRuntimeException() statement is added at the end of the catch statement so that aop catches the exception and rolls back, and the upper layer of service (webservice client, view layer action) continues to catch and handle the exception

Option 2. Add TransactionAspectSupport. currentTransactionStatus().setRollbackOnly(); Statement, which is rolled back manually so that the upper layer does not have to handle exceptions (as the project does now)

conclusion

That is the end of this article on spring transactional exception rollback instance resolution, and I hope you found it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: