简体   繁体   English

由另一个 class 调用的同一 class 中的方法调用的事务方法

[英]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.

相关问题 我的另一个类的方法没有被调用? - My method from another class is not being called? 无法从另一个类调用方法 - Method not being able to be called from another class 从另一个方法调用的@Transactional 方法不获取事务 - @Transactional method called from another method doesn't obtain a transaction 从同一类中的另一个安全方法调用安全方法时,@ PreAuthorize不起作用 - @PreAuthorize not working when a secured method is called from another secured method in same class 一类的方法未从另一类调用 - Method of one class not getting called from another class 如何存根从另一个类调用的类中的方法 - how to stub a method in a class which is called from another class 公共方法中的 LazyInitializationException 标记为 @Transactional 并从另一个 bean 调用 - LazyInitializationException in public method marked as @Transactional and called from another bean 从服务类中调用时,Spring @Transactional不适用于带注释的方法 - Spring @Transactional doesn't work for an annotated method when called from within service class 如果从一个类而不是从另一个类调用方法,则该方法有效 - Method works if called from one Class but not from another 如何模拟正在被同一类测试的另一个方法内部调用的类的方法? - How to mock a method of a class being called inside another method that is being tested of the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM