我已阅读有关 Spring 的官方文档
@Transactional(传播。需要)
注释,但仍有一些疑问。我将向您展示一个关于我认为它的行为方式的示例:
首次服务
public class MyServiceImpl implements MyService{
@AutoWired
private OtherService otherService;
@Transactional(Propagation.REQUIRED)
public void saveItem(Item item){.....}
@Transactional(Propagation.REQUIRED)
public void updateItem(Item item){....}
}
@Transactional(Propagation.REQUIRED)
public void deleteItem(Item item){
otherService.checkItem(item);
...........
}
}
二次服务
public class OtherServiceImpl implements OtherService {
@Transactional(Propagation.REQUIRED)
public void checkItem(Item item){.....}
}
从 Spring Controller 调用 MyServiceImpl 类:
如果我调用
saveItem()
,将创建一个新的物理和逻辑事务,对吗?如果我从控制器对该服务进行两次调用,一次调用
saveItem()
和下一次调用updateItem()
,Spring 将为每个方法创建两个物理不同的事务,对吗?如果我调用
deleteItem()
,将只创建一个物理事务,因为它会在调用 deleteItem 时打开一个事务,但是从这个方法的内部调用otherService.checkItem()
将重用第一个物理事务,对吗?