简体   繁体   中英

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. So I used REQUIRES_NEW to commit the inner transaction. 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 . Or can we have this propagation only in DAO layer?

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM