the*_*ngs 7 java spring transactions nested-transactions
我有一个@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)。但它会击败性能。
如果您不想在methodOne发生异常后回滚事务methodTwo,可以添加methodOne注释@Transactional(noRollbackFor = {RuntimeException.class})。但请注意,这有点滑坡,如果你真的想这样做,请三思而后行。
| 归档时间: |
|
| 查看次数: |
4773 次 |
| 最近记录: |