java.sql.SQLException: Can‘t call commit when autocommit=true
2020-07-28 23:38
2351 查看
问题
java.sql.SQLException: Can't call commit when autocommit=true at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:930) at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1535) at com.mchange.v2.c3p0.impl.NewProxyConnection.commit(NewProxyConnection.java:803) at com.itheima.utils.TransactionManager.commit(TransactionManager.java:35) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:643) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:625) at org.springframework.aop.aspectj.AspectJAfterReturningAdvice.afterReturning(AspectJAfterReturningAdvice.java:66) at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:53) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy26.transfer(Unknown Source) at com.itheima.test.AccountServiceTest.testTransfer(AccountServiceTest.java:21) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73) at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
分析
首先,说明一点。本问题的解决仅能解决使用了多个注解before,afterReturn,afterThrowing和after时,会出现的执行顺序错误的情况。
@Before("point()") public void beginTransaction(){ try { // connectionUtils.getThreadConnection().setAutoCommit(false); connectionUtils.getThreadConnection().setAutoCommit(false); }catch (Exception e){ e.printStackTrace(); } } @AfterReturning("point()") public void commit(){ try { connectionUtils.getThreadConnection().commit(); }catch (Exception e){ e.printStackTrace(); } } @AfterThrowing("point()") public void rollback(){ try { connectionUtils.getThreadConnection().rollback(); }catch (Exception e){ e.printStackTrace(); } } @After("point()") public void release(){ try { connectionUtils.getThreadConnection().close();//还回连接池中 connectionUtils.removeConnection(); }catch (Exception e){ e.printStackTrace(); } }
解决
- 使用环绕通知,或者XML配置通知
@Around("point()") public void around(ProceedingJoinPoint pj) throws SQLException { try { connectionUtils.getThreadConnection().setAutoCommit(false); Object p = pj.proceed(); connectionUtils.getThreadConnection().commit(); } catch (Throwable e) { connectionUtils.getThreadConnection().rollback(); throw new RuntimeException(e); }finally { connectionUtils.getThreadConnection().close();//还回连接池中 connectionUtils.removeConnection(); } }
相关文章推荐
- 解决报java.sql.SQLException: Can't call commit when autocommit=true异常
- Can't call rollback when autocommit=true
- Can't call commit when autocommit=true问题的解决方法
- Can't call rollback when autocommit=true
- Hive jdbc连接出现java.sql.SQLException: enabling autocommit is not supported
- ibatis java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timest
- java.sql.SQLException: Io 异常: Got minus one from a read call
- Caused by: java.sql.SQLException: Couldn't perform the operation commit
- 问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
- SQLException:value '0000-00-00' can not be represended as java.sql.TimeStamp
- java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
- java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
- java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
- Err:java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp
- 解决 java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented
- Linux下 Could not toggle autocommitjava.sql.SQLException解决方法
- java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Date
- 问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
- java.sql.SQLException: Value ‘0000-00-00 00:00:00’ can not be represented as java.sql.Timestamp
- java.sql.SQLException: Can not issue empty query.