@Transactional method called from another method doesn't obtain a transaction

后端 未结 3 1781
抹茶落季
抹茶落季 2020-11-27 17:48

In Spring, a method that is annotated with @Transactional will obtain a new transaction if there isn\'t one already, but I noticed that a transactional method

相关标签:
3条回答
  • 2020-11-27 18:28

    Yes, this is expected behaviour. @Transactional tells spring to create a proxy around the object. The proxy intercepts calls to the object from other objects. The proxy does not intercept calls within the object.

    If you want to make this work, add @Transactional on the method that is invoked from "outside".

    0 讨论(0)
  • 2020-11-27 18:42

    It is one of the limitations of Springs AOP. Because the dao bean is in fact a proxy when it is created by spring, it means that calling a method from within the same class will not call the advice (which is the transaction). The same goes for any other pointcut

    0 讨论(0)
  • 2020-11-27 18:49

    This is a bit late I know, but would just like to add a way to overcome this limitation is that within the method obtain the spring bean from the application context and invoke the method. When the spring bean is obtained from the application context it will be the proxy bean not the original bean . Since the proxy bean is now invoking the method instead of the original bean the transaction advice will be implemented on it.

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