我有一个 @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();
}
}
我希望将插入一个实体,但是如果任何嵌套事务抛出异常,即使在
AService.methodOne()
处处理了此异常,插入也会拒绝.我可以批注
methodTwo()
与 @Transactional(propagation = Propagation.REQUIRES_NEW)
.但它会击败性能。
最佳答案
如果您不想从 methodOne
回滚您的交易在 methodTwo
发生一些异常之后, 您可以添加注释 methodOne
与 @Transactional(noRollbackFor = {RuntimeException.class})
.但请注意,这有点滑坡,如果您真的想这样做,请三思。
关于java - 处理异常后Spring嵌套事务回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48462191/