Guice交易不会回滚错误

时间:2020-09-15 13:35:44

标签: java spring transactions guice

在评估Guice&Spring提供的Transaction支持时,我知道guice仅在Exception(默认情况下仅在RuntimeExceptions)上提供回滚策略-

Guice Documentation

Guice Code Pointer

private boolean rollbackIfNecessary(
      Transactional transactional, Exception e, EntityTransaction txn) {
    boolean commit = true;

    //check rollback clauses
    for (Class<? extends Exception> rollBackOn : transactional.rollbackOn()) {

      //if one matched, try to perform a rollback
      if (rollBackOn.isInstance(e)) {
        commit = false;

        //check ignore clauses (supercedes rollback clause)
        for (Class<? extends Exception> exceptOn : transactional.ignore()) {
          //An exception to the rollback clause was found, DON'T rollback
          // (i.e. commit and throw anyway)
          if (exceptOn.isInstance(e)) {
            commit = true;
            break;
          }
        }

        //rollback only if nothing matched the ignore check
        if (!commit) {
          txn.rollback();
        }
        //otherwise continue to commit

        break;
      }
    }

    return commit;
  }
}

尽管spring提供了即使发生错误也可以回滚的功能-

Spring Code Pointer

@Override
    public boolean rollbackOn(Throwable ex) {
        return (ex instanceof RuntimeException || ex instanceof Error);
    }

在我看来,如果应用程序出现任何错误-OutOfMemoryError / StackOverflowError-Spring将回滚事务,但是Guice不会。

我对这种理解是正确的还是缺少什​​么?还是我们不需要回退错误?

0 个答案:

没有答案