解决Transaction is already completed - do not call commit or rollback more than once per transaction问题

12 篇文章 0 订阅
11 篇文章 0 订阅

解决Transaction is already completed - do not call commit or rollback more than once per transaction问题

问题描述

系统自定义异常处理,在rollback时出现异常,日记如下所示

17:29:47 ERROR c.z.p.c.t.s.i.TmStatDetailServiceImpl - Transaction is already completed - do not call commit or rollback more than once per transaction
org.springframework.transaction.IllegalTransactionStateException: Transaction is already completed - do not call commit or rollback more than once per transaction
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:704) ~[spring-tx-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at 
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) [spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) [spring-tx-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) [spring-tx-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [spring-aop-4.2.1.RELEASE.jar:4.2.1.RELEASE]
        at com.sun.proxy.$Proxy223.downloadUsTmOfficialFile(Unknown Source) [na:na]

异常处理代码块如下所示

        DefaultTransactionDefinition definition=new DefaultTransactionDefinition();
        definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus transactionStatus=transactionManager.getTransaction(definition);
        try{
			.....
        }catch ( Exception e){
            transactionManager.rollback(transactionStatus);
        }finally {
            transactionManager.commit(transactionStatus);
        }

问题原因

经过查资料分析得知,出现该问题的主要原因是在系统出现异常rollback之后,又执行了finally的commit的方法。所以报错

解决方案

  1. rollback 执行完成之后,直接抛出异常退出当前方法
  2. 去掉finaly模块,将commit前移到catch之前 或者catch之后都可以。
        DefaultTransactionDefinition definition=new DefaultTransactionDefinition();
        definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus transactionStatus=transactionManager.getTransaction(definition);
        try{
			.....
			 transactionManager.commit(transactionStatus);
        }catch ( Exception e){
            transactionManager.rollback(transactionStatus);
            throw e;
        }
       
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值