关于java:javasqlSQLException-Cant-call-commit-when-autocommittrue

63次阅读

共计 3638 个字符,预计需要花费 10 分钟才能阅读完成。

在应用 java 操作数据库, 创立数据库连贯对象, 创立事务管理器(开启事务, 提交事务, 回滚事务), 运行代码后, 报错如下

java.sql.SQLException: Can’t call commit when autocommit=true

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:65)
at com.mysql.cj.jdbc.ConnectionImpl.commit(ConnectionImpl.java:814)
at com.alibaba.druid.pool.DruidPooledConnection.commit(DruidPooledConnection.java:749)
at com.bigdata.utils.TransactionManager.commit(TransactionManager.java:29)
at com.bigdata.service.impl.AccountServiceImpl.transfer(AccountServiceImpl.java:29)
at com.bigdata.test.SpringTansferTest.test(SpringTansferTest.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:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
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:366)
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$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
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$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
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)

相干代码如下

谬误提醒重点在于第一句, 意思是如果以后主动提交事务的性能是开启的, 就不能调用 commit 办法.

然而就像我下面的代码, 实际上在 begin() 办法中曾经将主动提交事务的性能敞开了 (setAutoCommit(false)), 而在另外的代码中也能够保障在提交事务的办法commit() 执行之前, 先执行 begin() 办法

最初发现起因是创立数据库连贯对象那里, 从连接池获取数据库连贯对象是在成员办法 getConnection() 中实现的. 那么每次调用这个办法时, 不能保障两次获取的是同一连贯. 所以在以后连贯敞开了主动提交事务, 然而下一个连贯不肯定是这种状况.

所以解决形式就是将从连接池获取连贯的操作移到成员办法外, 确保应用的是同一连贯

正文完
 0