java - spring 1.2升级到spring 4.3.9后事务回滚异常

标签 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>

最佳答案

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

PROPAGATION_REQUIRED documentation 中所述,这种行为是正常的

However, in the case where an inner transaction scope sets the rollback-only marker, the outer transaction has not decided on the rollback itself, and so the rollback (silently triggered by the inner transaction scope) is unexpected. A corresponding UnexpectedRollbackException is thrown at that point. This is expected behavior so that the caller of a transaction can never be misled to assume that a commit was performed when it really was not. So if an inner transaction (of which the outer caller is not aware) silently marks a transaction as rollback-only, the outer caller still calls commit. The outer caller needs to receive an UnexpectedRollbackException to indicate clearly that a rollback was performed instead.

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

关于java - spring 1.2升级到spring 4.3.9后事务回滚异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418511/

相关文章:

java - InvalidClassException 不兼容的类 (SUID)

spring - Tomcat Axis2 - 没有看到 Spring jar

java - 使用 Spring 重试进行某些重试后,将有毒消息推送到回退队列 [IBM MQ]

java - hibernate 随机 "Session is closed error"与 2 个数据库

java - 同步和@Transactional 让我困惑

java - (ObjectOptimisticLockingFailureException) 单个事务中的 StaleStateException

java - 从多个线程写入文本文件?

java - 诊断 "RuntimeException: Unable to start activity Caused by: java.lang.NullPointerException"

触发了 hibernate 实体拦截器但设置值不保存

java - Struts 2 - JQuery 插件 : Spinner Value change event