有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Spring中使用哪种传播?

我有两个不同的进程(A和B),A必须在B之后启动,B不能加入A的事务,B必须等到A完成其提交

我应该用什么

现在它就像:

@Transactional
A()

@Transactional
B()

现在我使用它defult@Transactional,它不能正常工作。我想我应该使用传播

我希望问题是清楚的。 提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    如果B()在其事务上下文中调用A(),那么A()应该使用REQUIRES_NEW传播来拥有自己的独立事务。 enter image description here

    根据spring框架的Data Access Section

    PROPAGATION_REQUIRES_NEW, in contrast to PROPAGATION_REQUIRED, uses a completely independent transaction for each affected transaction scope. In that case, the underlying physical transactions are different and hence can commit or roll back independently, with an outer transaction not affected by an inner transaction’s rollback status.

  2. # 2 楼答案

    enter image description here

    这张照片告诉你为什么你应该使用新的。您想要的是调用总是在新事务(TX)上下文中发生

    但是请注意:实际的事务暂停不会在所有事务管理器上都适用。如果您正在使用JtaTransactionManager,则需要使javax.transaction.TransactionManager对其可用