我正在尝试创建一个事务方法,它调用其他几个事务方法以保存一些相互依赖的数据库实体。如果任何调用失败,我希望事务完全回滚。然而,这不是观察到的行为。这是我的代码:
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public void save(EntityToBeSaved entity) {
try{
for(SubEntity sub: entity.getSubEntities()) //specifics omitted
saveSubEntity(sub); //this is transactional
}
catch (DataIntegrityViolationException e){
throw new BusinessException("Duplicate Name");
}
}
saveSubEntity
也有Propagation.REQUIRED
and rollobackFor = Throwable.class
,但是当事务在第二次saveSubEntity
调用失败时,第一次subEntity
被提交。