Spring transaction exception rollback, capture exceptions do not throw will not roll back, spring transaction

Source: Internet
Author: User

Spring transaction exception rollback, capture exceptions do not throw will not roll back, spring transaction

I recently encountered a situation where transactions do not roll back. I also want to say that there is a bug in JPA transactions? I think more .......
To print logs Clearly, I add tyr catch to many methods and print logs in catch. However, when this method is abnormal, logs are printed, but the added transactions are not rolled back.

Example:
Methods like this will not be rolled back (one method has an error and the other method will not be rolled 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. }
  8. }


The following method rolls back (one method has an error and 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. }
[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 ?? I don't understand the transaction mechanism of spring .!!
By default, spring transactions are rolled back only when uncaptured runtimeexcetpion occurs.
Spring aop exception capture principle: the intercepted method must explicitly throw an exception and cannot be processed. In this way, the aop agent can capture the exception of the method and perform rollback, by default, aop only captures runtimeexception exceptions.

Configuration to capture specific exceptions and roll back
In other words, do not use try catch in the service method or add throw new runtimeexcetpion () in the catch. In this way, the program can be caught by aop and rolled back only when the program is abnormal.
Solution:
Solution 1. for example, when the service layer processes transactions, the methods in the service do not capture exceptions, or add the throw new RuntimeException () Statement to the catch statement to allow aop to capture exceptions and then roll back, in addition, at the upper layer of the service (webservice client, view layer action), you must continue to capture and handle this exception.
Solution 2. add TransactionAspectSupport to the catch statement of the service-layer method. currentTransactionStatus (). setRollbackOnly (); statement, manual rollback, so that the upper layer does not need to handle exceptions (current project practices)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.