@Transactional(propagation = Propagation.REQUIRED)在春季?

问题描述:

如果我有以下代码:

@Component
public class A{ 

@Transactional(propagation = Propagation.REQUIRED)
public void a(){
    //logic
    b();
 //logic
}

@Transactional(propagation = Propagation.REQUIRED)
public void b(){
    //logic
} 
}

在此代码示例中有多少笔交易打开了Spring?

How many transactions open Spring in this code example?

没关系.从a()调用b()时,它将不会通过代理,因此不会考虑b()上的任何事务属性.

It doesn't matter. When calling b() from a() it won't be going through the proxy, so any transactional attributes on b() won't be considered.

如果通过代理(即在类外部)调用a()b(),示例代码将打开1个事务,并且尚无正在进行的事务.

The example code has 1 transaction open if a() or b() is called through the proxy (i.e. outside of the class) and there isn't a transaction in progress already.