Strange behaviour with @Transactional(propagation=Propagation.REQUIRES_NEW)

后端 未结 1 1246
北荒
北荒 2020-12-03 01:50

Here is my problem :

I\'m running a batch on a Java EE/Spring/Hibernate application. This batch calls a method1. This method calls a <

相关标签:
1条回答
  • 2020-12-03 02:09

    Spring transactions, by default, work by wrapping the Spring bean with a proxy which handles the transaction and the exceptions. When you call method2() from method1(), you're completely bypassing this proxy, so it can't start a new transaction, and you're effectively calling method2() from the same transaction as the one opened by the call to method1().

    On the contrary, when you call a method of another injected bean from method1(), you're in fact calling a method on a transactional proxy. So if this alien method is marked with REQUIRES_NEW, a new transaction is started by the proxy, and you're able to catch the exception in method1() and resume the outer transaction.

    This is described in the documentation.

    0 讨论(0)
提交回复
热议问题