我有一个使用 Hibernate 连接到数据库的 JavaEE 应用程序。在我的应用程序的某些部分,我调用了具有@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
}
}