在 Spring 中使用哪种传播?

Which propagation to use in Spring?

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

我应该使用什么传播方式?

现在是这样的:

@Transactional
A()

@Transactional
B()

现在我默认使用它@Transactional,但它不能正常工作。我想我应该使用 PROPAGATION.

我希望问题很清楚。 提前致谢。

如果 B() 在其事务上下文中调用 A(),则 A() 应该使用 REQUIRES_NEW 传播以拥有自己的独立事务。

根据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.

这张照片几乎告诉了您为什么要使用 REQUIRES_NEW。你想要的是调用总是发生在新的交易(TX)上下文中。

但请注意:实际的事务暂停不适用于所有事务管理器的开箱即用。如果您正在使用 JtaTransactionManager,则需要 javax.transaction.TransactionManager 可用。