在 spring 容器中,代码如下:
public class A {
@Transactional
public void m1() {
...
b.m2(); // call in a new transaction
...
}
}
public class B {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void m2() {
...
}
}
为 m2()
创建的事务究竟何时被提交? 一旦 m2()
调用结束,或者一旦 m1( )
调用结束?
When does @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) commit?为 EJB 回答它,但它似乎与 JPA 的行为不同。
我调试了它,我只能在 m1()
结束后看到 m2()
对数据库的影响,但这对我来说似乎很奇怪,我是不是错过了什么在这里?
更新:
我将在 m1()
中检索到的实体传递给 m2()
并从那里更新它。
所以,实际上在 m2()
中合并实体解决了这个问题,Mik378 答案是正确的。
最佳答案
来自 here :
Whether you're using the Spring Framework or EJB, use of the REQUIRES_NEW transaction attribute can have negative results and lead to corrupt and inconsistent data.
The REQUIRES_NEW transaction attribute always starts a new transaction when the method is started, whether or not an existing transaction is present.
REQUIRES_NEW
启动新事务,即使上下文中存在现有事务也是如此。
所以简短的回答是:一旦 m2()
调用结束
关于java - JPA REQUIRES_NEW 事务到底什么时候提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22348539/