假设我在某处有注释的方法@Transactional
(因此开始事务)并且在里面做几个saveAndFlush()
. 也许我什至调用了一些依赖项,该依赖项具有通过被注释来启动它自己的(子?嵌套?)事务的方法@Transactional
。如果事务在两者之间的某个地方失败,什么会回滚?
一些例子:
public class ComplexProcess {
@Inject
private DAO<SomeEntity> someEntityDao;
@Inject
private SomeManagerClass manager;
@Transactional
public void doStuff(SomeEntity entity) {
manager.createAnObjectDependingOnEntity(entity);
entity = someEntityDao.saveAndFlush(entity); // what if this fails?
manager.doMoreStuffDependingOnEntity(entity);
}
}
public class SomeManagerClass {
@Inject
private DAO<SomeObject> someObjectDao;
@Inject
private DAO<SomeOtherEntity> someOtherEntityDao;
// what if this fails?
@Transactional // what if this isn't there?
public void createAnObjectDependingOnEntity(SomeEntity entity) {
SomeObject someObject = createSomeObjectDependingOnEntity(entity);
someObjectDao.saveAndFlush(someObject);
}
// what if this fails?
@Transactional // what if this isn't there?
public void doMoreStuffDependingOnEntity(SomeEntity entity) {
SomeOtherEntity someOtherEntity = whatever(entity);
someOtherEntityDao.safeAndFlush(someOtherEntity);
}
}
再说一遍:如果说最后一步失败了,hibernate 还会回滚之前发生的一切doStuff()
吗?因为我有一种感觉,它没有。如果这个完全回滚是不可能的saveAndFlush()
,那么在事务结束时使用save()
并等待自动刷新会有所帮助吗?我刚刚读到,即使在离开@Transactional
块之前,休眠也会在其之间自行进行一些无法控制的刷新?!