简体   繁体   English

Spring交易REQUIRES_NEW是否会传播到方法中的方法?

[英]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 ,数据也不会在method3method1提交。 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.

相关问题 Spring事务REQUIRED与REQUIRES_NEW:回滚事务 - Spring transaction REQUIRED vs REQUIRES_NEW : Rollback Transaction REQUIRES_NEW在Spring + Hibernate中不创建新交易 - REQUIRES_NEW not creating a new transaction in spring+hibernate Spring真的用REQUIRES_NEW开始新的交易吗? - Does Spring actually start a new transaction with REQUIRES_NEW? Spring 事务:从带有 requires 的方法调用的带有 requires_new 的方法 - Spring transactions: Method with requires_new called from a method with requires Spring 事务从 NOT_SUPPORTED 到 REQUIRES_NEW 的传播 - Spring Transaction Propagation from NOT_SUPPORTED to REQUIRES_NEW 在 Spring Boot 的 for 循环中使用 REQUIRES_NEW 的事务问题 - Transaction problem using REQUIRES_NEW in a for loop with Spring Boot EJB 调用的 Spring 事务:当 Try 的事务方法出现 RuntimeException 时,Catch 中的事务方法需要 REQUIRES_NEW - Spring transaction called by EJB: REQUIRES_NEW needed for transactional method in Catch, when Try's transactional method has RuntimeException 要求在REQUIRES_NEW内的REQUIRES_NEW内......等等 - REQUIRES_NEW within REQUIRES_NEW within REQUIRES_NEW … on and on 代理的REQUIRES_NEW方法不会回滚(春季) - Proxied REQUIRES_NEW method doesn't rollback (spring) 为什么不提交 Requires_New 的事务? - Why does not commit transaction of Requires_New?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM