Spring Transactions - Prevent rollback after unchecked exceptions (RuntimeException)

前端 未结 1 1974
情深已故
情深已故 2021-01-05 15:27

I can\'t manage to prevent a transaction from rollingback after a RuntimeException. My env is Spring 4.1 + Hibernate 3.6 + JTA (WebSphereUowTransactionManager) running on We

相关标签:
1条回答
  • 2021-01-05 15:59

    As far as I know, as soon as a runtime exception is thrown from a transactional method and is intercepted by the transaction interceptor, the transaction is marked as rollback only. Even if this transactional method is called from another transactional method.

    This makes sense to me: if the inner method can't recover from an exception, it can't recover, and an outer method shouldn't do as if nothing happened.

    If you're expecting the transaction not to rollback, you could

    • make the inner method non-transactional
    • configure the inner method not to rollback on this exception
    • have two inner methods:
      • one that is transactional, and is intended to be called when there is no transaction yet, and which simply delegates to the second one
      • one which is not transactional, and is intended to be called as part of an already existing transaction
    0 讨论(0)
提交回复
热议问题