简体   繁体   English

在另一个实例中从事务方法调用 spring 非事务方法时,事务是否得到传播?

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

相关问题 事务回滚后调用非事务方法 - Calling non-transactional method after transaction is rolled back 如何在相同和不同服务中调用@Transactional和non-Transactional方法时回滚事务? - How to rollback transaction on calling @Transactional and non-Transactional method in same and different Service? 如何从事务方法调用非事务方法 - How to call non-transactional methods from a Transactional method Spring Boot-从非事务性原因更新中校准事务性方法 - Spring Boot - caling transactional method from non-transactional cause update 从非事务方法调用的多个事务方法的传播级别 - Propagation level for multiple transaction methods called from a non-transactional method 是否可以将整个@Transactional类的一个方法标记为非事务性的 - Is it possible to mark one method of an entire @Transactional class as non-transactional 在Spring中是否可以从非事务方法中调用事务方法? - Is it possible to invoke transactional method from non transactional method in Spring? Spring @Transactional方法 - 参与交易 - Spring @Transactional method - participating transaction 当来自另一个事务的延迟加载的 Hibernate 对象被传递给方法时,Spring @Transactional 不起作用 - Spring @Transactional not working when Hibernate object with lazy loading coming from another transaction is passed down to method 从@transactional公共方法调用私有方法时,将在私有方法中使用同一事务还是不使用任何事务 - when calling private method from @transactional public method, will the same transaction be used in private method or no transaction used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM