[英]Will Spring transaction REQUIRES_NEW be propagated to the methods within the method?
My requirement is to commit/rollback changes from method2
and not be dependent on the outer transaction. 我的要求是从
method2
提交/回滚更改,而不依赖于外部事务。 So I used REQUIRES_NEW
to commit the inner transaction. 因此,我使用
REQUIRES_NEW
提交了内部事务。 But I need some clarification as to the type of propagation the other methods will hold when called from the method. 但是我需要澄清一下其他方法从该方法调用时将保持的传播类型。
@Transactional(propagation=Propagation.REQUIRES_NEW)
public String method1(long id) {
ABC obj = method2(id);
method3(obj);
myDAO.saveOrUpdate(obj);
}
private ABC method2(long id) {
ABC obj1 = myDAO.readData(id);
...
...
return obj1;
}
private void method3(ABC obj) {
ABC obj1 = (ABC)obj.clone();
obj1.setId(123);
obj1.setName("Name");
myDAO.persist(obj1);
}
Now the issue is the data is not committed in method3
and method1
, even after setting the propagation as REQUIRES_NEW
. 现在的问题是,即使将传播设置为
REQUIRES_NEW
,数据也不会在method3
和method1
提交。 Or can we have this propagation only in DAO layer? 还是只能在DAO层中进行这种传播?
As i see in your code the method2(..) belong to same object calling method1(..) and that cause the method2() to use the same new transaction created by method1, this behaviour is forced by the proxy mode spring use i quote: 正如我在您的代码中看到的那样,method2(..)属于调用method1(..)的同一对象,并且导致method2()使用由method1创建的相同新事务,此行为是由代理模式spring use i强制执行的引用:
In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted.
在代理模式(默认设置)下,仅拦截通过代理传入的外部方法调用。 This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional
这意味着自调用实际上是目标对象中的一种方法,它调用目标对象的另一种方法,即使调用的方法标记有@Transactional,也不会在运行时导致实际事务。
To make two transactions you must create the second method in another object and annotated with @Transactional(propagation=Propagation.REQUIRES_NEW)
to have the desired effect 要进行两次交易,必须在另一个对象中创建第二个方法,并用
@Transactional(propagation=Propagation.REQUIRES_NEW)
进行注释才能达到预期的效果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.