在调用方法中,使用传播= Propagation.SUPPORTS不能进行事务回滚

时间:2018-10-23 08:48:38

标签: java spring hibernate

我有此代码:

@Service
public class ServiceA {

  @Autowired
  private SomeDao someDao;

  @Autowired
  private SomeService someService;

  @Transactional
  public void methodA() {
    someDao.saveEntity(entity);
    // inside saveEntity():
    //     Session sess = getCurrentSession();
    //     Long id = (Long) sess.save(entity);
    //     sess.flush();
    //     sess.clear();
    someService.getToken();
    // "getToken()" annotated with
    // @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    throw new RuntimeException("some exception!");
  }

当我调用“ methodA()”时,日志中显示事务已回滚:

TransactionInterceptor        - Completing transaction for [...methodA] after exception: java.lang.RuntimeException: some exception!
RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on java.lang.RuntimeException: some exception!
RuleBasedTransactionAttribute - Winning rollback rule is: RollbackRuleAttribute with pattern [java.lang.Exception]
HibernateTransactionManager   - Triggering beforeCompletion synchronization
HibernateTransactionManager   - Initiating transaction rollback
HibernateTransactionManager   - Rolling back Hibernate transaction on Session [SessionImpl(...)]

但是实际上交易不会回滚,并且记录会出现在数据库中。

如果我将方法“ someService.getToken()”的注释更改为“ @Transactional(propagation = Propagation.REQUIRES_NEW)”,则日志几乎相同(除了外部事务被挂起和为“ someService.getToken()”方法创建了一个新对象,并在完成后将其提交),但该事务确实已回滚

数据库:oracle 11g

依赖版本:

<org.springframework-version>4.2.5.RELEASE</org.springframework-version>
<hibernate.version>4.3.11.Final</hibernate.version>

交易管理器:

<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

0 个答案:

没有答案