[英]When calling a spring Non-Transactional method from a Transactional Method in another instance, does the transaction get propagated?
Sorry if this has been discussed before, but I just could not find the answer in all the related discussions on @Transactional spring java annotation.对不起,如果之前已经讨论过,但我在@Transactional spring java 注释的所有相关讨论中找不到答案。
What happens if we call a method in another instance that does not have an @Transactional annotation from a method that has an @Transactional annotation?如果我们从具有 @Transactional 注释的方法调用另一个没有 @Transactional 注释的实例中的方法,会发生什么? will the transaction from the @Transactaional annotated method be suspended?
来自@Transactaional 注释方法的事务会被暂停吗? or would it be propagated?
还是会被传播?
I found a lot of discussions on if the call to the 2nd method is from the same instance, but not if the 2nd non-transactional method is in another instance.我发现了很多关于对第二个方法的调用是否来自同一个实例的讨论,而不是第二个非事务性方法是否在另一个实例中。 Thank you for your help!
感谢您的帮助!
Like in the example below, what would happen if methodb() in class B is called from methoda() in class A?就像下面的例子,如果从类 A 中的 methoda() 调用类 B 中的 methodb() 会发生什么? Would the transaction from methoda be propagated or would methodb be non-transactional?
来自methoda的事务会被传播还是methodb是非事务性的?
Example code below:示例代码如下:
public class A
{
@Autowired
B b;
@Transactional
public void methoda(){
b.methodb();
}
}
@Component
public class B
{
//this method is non transactional and is called from method A in class A
public void methodb()
{
//do some db updates
}
}
If there are no @Transactional annotations in B to tell it otherwise, then the b method uses the transaction established in the call to A.methoda.如果 B 中没有 @Transactional 注释来告诉它其他情况,那么 b 方法使用在对 A.methoda 的调用中建立的事务。 This is normal for Spring, we have components marked transactional that call DAOs that participate in the current transaction without being marked transactional themselves.
这对于 Spring 来说是正常的,我们有标记为事务性的组件,它们调用参与当前事务的 DAO,而本身没有被标记为事务性。
If you really want to suspend the current transaction, you can add this annotation to the b method:如果你真的想挂起当前的事务,你可以在b方法中添加这个注解:
@Transactional(propagation = Propagation.NOT_SUPPORTED)
There is a caveat about suspending transactions in the Spring API doc: Spring API 文档中有一个关于暂停事务的警告:
NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers.
注意:实际的事务暂停不会在所有事务管理器上开箱即用。 This in particular applies to JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available to it (which is server-specific in standard Java EE).
这尤其适用于 JtaTransactionManager,它需要 javax.transaction.TransactionManager 对其可用(在标准 Java EE 中是特定于服务器的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.