我有一个@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)。但它会击败性能。