【问题标题】:Spring Transactional annotation causes unwanted rollback-onlySpring Transactional annotation 仅导致不需要的回滚
【发布时间】:2013-01-13 02:21:10
【问题描述】:

我们使用的是 Spring 3.2.0。我们的服务类都有带有@Transactional 注释的*ServiceImpl,例如在bean vatcodeServiceTarget 上:

@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public VatCodeModel loadBy(String crsname, String crscode, int clientid) {
  ..
}

我们用 TransactionProxyFactoryBean 包装 ServiceImpl

<bean id="vatcodeService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="target" ref="vatcodeServiceTarget"/>
    <property name="transactionAttributeSource">
        <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
    </property>
</bean>

并将生成的 bean 注入我们的控制器。如果另一个 ServiceImpl 需要 'vatcodeService',我们实际上会注入 'vatcodeServiceTarget' 所以事务边界被丢弃。

这种方法在 Spring 2.0 中运行良好。现在,在 Spring 3.2.0 中,我观察到以下效果:

  1. 控制器调用另一个 ServiceImpl,事务性
  2. 其他ServiceImpl 调用VatcodeServiceImpl#loadBy()。 loadBy() 调用没有包裹事务边界
  3. VatcodeServiceImpl#loadBy() 未能加载查找的数据库行并引发 DataNotFoundException
  4. 另一个 ServiceImpl 中的事务现在被标记为仅回滚。

看起来跨越 @Transactional 边界的 RuntimeException 足以标记 tx 仅回滚,即使该特定调用实际上不是事务性的。

我是不是配置有问题,还是 Spring @Transactional 方面在这里表现异常?

谢谢 西蒙尼德伯格

【问题讨论】:

  • VatCodeServiceImpl 是否在任何组件扫描路径中?
  • @ach:我们当时没有使用context:component-scan,但VatCodeServiceImpl在Spring上下文中被定义为一个bean。

标签: spring spring-transactions


【解决方案1】:

为什么你认为没有交易边界?您添加了注释 - 春天添加了方面,它打开事务并将其标记为仅在运行时异常时回滚。如果您真的不想回滚此事务,您可能会抛出一些已检查的异常而不是未检查的异常,但我不明白您为什么要这样做。

【讨论】:

    猜你喜欢
    • 2013-09-16
    • 2017-04-14
    • 2018-11-08
    • 2019-06-30
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    相关资源
    最近更新 更多