繁体   English   中英

处理@Transactional类中的异常

[英]Handling Exceptions within @Transactional Class

在我的Spring托管类( @Transactional@Service annotations)中,我有以下方法:

@Transactional
@Service
public class Foo {

    @Autowired
    private MyService service; // MyService has @Service and @Transactional annotations


      public void save(...) { 
          try { 
              service.save(...); // gets a Hibernate Session & calls `merge(...)`
          } catch(DataIntegrityViolationException e) {
             logMessage("save failed with exception: " + e);
          }
      }

在我对应的表中( Foo#save用于Foo#save工作),我有一个unique constraint

当上面的Foo#save代码以违反唯一约束的方式执行时,我在我的日志中看到抛出了DataIntegrityViolationException ,但它没有被我的Foo#save DataIntegrityViolationExceptioncatchcatch

   org.springframework.orm.jpa.JpaTransactionManager 891 doRollbackOnCommitException - 
     Initiating transaction rollback after commit exception 
   org.springframework.dao.DataIntegrityViolationException: Could not execute 
       JDBC batch update; SQL [insert into MY_TABLE ...; constraint 
         [MY_CONSTRAINT]; nested exception is 
       org.hibernate.exception.ConstraintViolationException: Could not execute 
    JDBC batch update

感到困惑的是没有捕获到DataIntegrityViolationException ,我为所有RuntimeException添加了另一个catch:

      public save(...) { 
          try { 
              service.save(...); // `service` is marked with 
                                 //  @Service and @Transactional
          } catch(DataIntegrityViolationException e) {
             logMessage("save failed with exception: " + e);
          } catch(RuntimeException e) {
             logMesssage("runtime exception e: " + e);
          }
      }

但是,当我运行违反唯一约束违规的相同代码时,我没有看到来自catch(RuntimeException e)块的日志消息。

我的理解是,由于我使用的是@Transactional ,因此Foo的代理(由Spring创建)将执行save方法调用。

但是,我有可能捕获service.save(...)调用的DataIntegrityViolationException吗?

方法session.merge(...)不会立即执行插入/更新SQL查询,而是将此操作放入队列中。 会话刷新时将执行SQL查询。 在您的情况下,在您的方法执行后,会话将在事务性建议中刷新,并且异常将被抛出。

所以要在你的方法中捕获异常,请在session.merge(...)之后添加session.flush() ,如下所示:

  public class MyService {

      public void save(Object o) {
         Session session = //get session somehow
         session.merge(o);
         session.flush();  // In case of data integrity violation Exception will be thrown here
      }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM