我有一个JavaEE应用程序使用Hibernate连接到数据库。在我的应用程序的某些部分,我调用了具有@Transactional
注释的方法。在其中一些情况下,我想回滚整个事务(外部服务方法调用和内部)。在某些情况下,我只想回滚内部服务方法调用(即回滚到在内部方法开始时定义的保存点)。如何使用休眠回滚到保存点嵌套事务
第一部分已经存在,但第二部分有问题。当我执行以下操作时,出现“UnexpectedRollbackException”,并显示消息“事务已回滚,因为它已被标记为仅回退”。
@Service
public class OuterService{
@AutoWired
private InnerServcie innerService;
@Transactional
public void outer(){
try{
innerService.inner();
}catch(RuntimeException e){
//if i dont throw this up, it will give me the "UnexpectedRollbackException"
System.out.println("I cought a RuntimeException");
}
}
}
@Service
public class InnerServcie{
@Transactional
public void inner(){
//here we insert some data into db using hibernate
//but something goes wrong and an exception is thrown
}
}
我已将此添加到内部方法,我也得到一个僵局!有任何想法吗? – hfm