[英]Transactional method called by method in same class called from another class
I have a transactional method that is called called multiple time (in a loop) by a method in the same class.我有一个事务方法,它被同一个 class 中的方法调用多次(在循环中)。 This method in the same class is called by a method in a different class.
同一 class 中的此方法由不同 class 中的方法调用。 From my investigation, when we call a transaction method within the same class, this has no effect.
根据我的调查,当我们在同一个 class 中调用事务方法时,这没有效果。 The same transaction is used.
使用相同的事务。 But in my case, will a new transaction be used?
但就我而言,是否会使用新的交易?
Structure:结构:
A.class - Method A
|-> B.class - Method A
|-> call mutiple times - B.class - @Transactional Method B
Can you advise?你能建议吗?
My expectations is that a new transaction on Method B is created each time I loop through it.我的期望是每次循环遍历方法 B 时都会创建一个新事务。 Do I need to pass the Method B to a different class?
我是否需要将方法 B 传递给不同的 class? Or it will start a new transaction every time?
还是每次都会开始新的交易?
So BY DEFAULT two things are potentially working against you.因此,默认情况下,有两件事可能对您不利。
Spring's default txn interceptors, which is how AOP transactions work, will not pick up calls within the same instance. Spring 的默认 txn 拦截器,这是 AOP 事务的工作方式,不会在同一实例中接收调用。 This can be changed by switching to AspectJ weaving.
这可以通过切换到 AspectJ weaving 来改变。 This article has a good explanation https://www.baeldung.com/spring-aop-vs-aspectj IIRC the default in spring is jdk proxying.
这篇文章有一个很好的解释https://www.baeldung.com/spring-aop-vs-aspectj IIRC spring 中的默认是 jdk 代理。
Spring's default transactional annotation will propagate existing txns. Spring 的默认事务注释将传播现有的 txns。 So if there's not a pre-existing txn AND you overcome the above you should get what you want.
因此,如果没有预先存在的 txn 并且您克服了上述问题,您应该得到您想要的。 If there's a pre-existing txn, it will by default be used, but this behaviour can be changed by modifying the
propagate
parameter on your @Transactional
annotation.如果存在预先存在的 txn,默认情况下会使用它,但可以通过修改
@Transactional
注释上的propagate
参数来更改此行为。 This article has a good explanation: https://www.baeldung.com/spring-transactional-propagation-isolation这篇文章有很好的解释: https://www.baeldung.com/spring-transactional-propagation-isolation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.