将Spring 1.2升级到Spring 4.3.9后,事务回滚了异常

时间:2019-01-29 10:07:41

标签: java spring spring-transactions

我们已经将Spring版本升级到4.3.9,并且在升级之后,我们得到了事务回滚异常

org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:125)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy65.bulkDeleteAreas(Unknown Source)
at com.nis.compass.test.functional.geocoding.GeocodingManagerTestHelper.bulkDeleteAreas(GeocodingManagerTestHelper.java:188)

这是Bean和父transactionProxy Bean的xml配置。 我们没有更改xml中的任何内容,但仍然会引发事务异常。

  <bean id="geocodingManager" parent="baseTransactionProxy">
<property name="target">
  <bean class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>com.nis.compass.geocoding.service.GeocodingManagerInternal</value>
    </property>
    <property name="target" ref="geocodingManagerImpl" />
    <!-- list of interceptors, "Advice"s, and Advisors to add to the interceptor chain -->
    <property name="interceptorNames">
      <list>
        <value>performanceMonitorProxy</value>
      </list>
    </property>
  </bean>
</property>

  <bean id="baseTransactionProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
  <ref local="compassTransactionManager" />
</property>
<property name="transactionAttributes">
  <props>
    <!-- Always propagate transaction, rolling back on any checked or unchecked exception -->
    <prop key="*">PROPAGATION_REQUIRED,-Throwable</prop>
  </props>
</property>
<property name="preInterceptors">
  <list>
    <bean class="com.nis.compass.common.interceptor.ExceptionTranslationInterceptor" />
  </list>
</property>

1 个答案:

答案 0 :(得分:1)

我认为这只是两个版本之间的日志改进。功能行为可能是相同的。

这种行为是正常的,如PROPAGATION_REQUIRED documentation

中所述
  

但是,在内部事务范围设置仅回滚标记的情况下,外部事务尚未决定回滚本身,因此回滚(由内部事务范围默默触发)是意外的。此时将引发相应的UnexpectedRollbackException。 这是一种预期的行为,因此,交易调用者永远不会被误认为是在确实未执行提交的情况下发生的。因此,如果内部事务(外部调用方不知道)将事务无提示地标记为仅回滚,则外部调用方仍会调用commit。外部调用者需要接收一个UnexpectedRollbackException,以清楚地指示已执行回滚。

您还可以阅读这篇相关的文章,总结了文档中的内容:UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only