小编the*_*ngs的帖子

Spring嵌套事务​​处理异常后回滚

我有一个@Service类,其中有一个@Transactional方法可以调用@Transactional另一个服务上的另一个方法。像这样的东西:

@Service
public class AService {
  @Autowired
  BService b;
  @Autowired
  ARepository aRepo;

  @Transactional
  public void methodOne(){
    try{
      b.methodTwo();
    }catch(RuntimeException e){}
    aRepo.save(new A());
  }

} 

@Service
public class BService{

    @Transactional
    public void methodTwo(){
      if(true)
        throw new RuntimeException();
    }

}
Run Code Online (Sandbox Code Playgroud)

我期望 A 实体将被插入,但如果任何嵌套事务抛出异常,插入将拒绝,即使此异常已在 处处理AService.methodOne()

我可以methodTwo()用进行注释@Transactional(propagation = Propagation.REQUIRES_NEW)。但它会击败性能。

java spring transactions nested-transactions

7
推荐指数
1
解决办法
4773
查看次数

标签 统计

java ×1

nested-transactions ×1

spring ×1

transactions ×1