Can't call commit when autocommit=true问题的解决方法
来源:互联网 发布:简单pdf解密软件 编辑:程序博客网 时间:2024/06/01 20:38
今天写java爬虫将爬取数据保存到数据库的时候总是报错 Can't call commit when autocommit=true,从而无法将数据成功保存。
因为这一问题纠结了很久,终于找到解决办法。
具体的报错信息如下:
java.sql.SQLException: Can't call commit when autocommit=true
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:868)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:864)
at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1540)
at document.Storage.process(Storage.java:131)
at us.codecraft.webmagic.Spider.onDownloadSuccess(Spider.java:418)
at us.codecraft.webmagic.Spider.processRequest(Spider.java:406)
at us.codecraft.webmagic.Spider.access$000(Spider.java:61)
at us.codecraft.webmagic.Spider$1.run(Spider.java:320)
at us.codecraft.webmagic.thread.CountableThreadPool$1.run(CountableThreadPool.java:74)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
上面蓝色字体指出了我的代码错误位置,错误代码如下:
preparedStatement.executeUpdate();一看这句代码,可以肯定的是,这句代码是没有错误的,但是还是报错了。
Can't call commit when autocommit=true表示的意思就是当自动属性值为true是无法call commit(mysql默认都是自动提交)
解决此问题只需要将自动提交改成手动提交即可,也就是令autocommit=false。只需在代码中加入以下语句即可:
conn.setAutoCommit(false);其中conn是Connection对象。
切记:造成这个报错的原因是用到了“回滚”(commit或者rollback),使用回滚的前提是mysql的自动提交处于关闭状态。
- Can't call commit when autocommit=true问题的解决方法
- java.sql.SQLException: Can't call commit when autocommit=true
- java.sql.SQLException: Can't call commit when autocommit=true
- 解决报java.sql.SQLException: Can't call commit when autocommit=true异常
- Can't call rollback when autocommit=true
- Can't call rollb ack when autocommit=true
- 遇到的问题-----mongodb.MongoException$Network: can't call something
- MongoDB can't call something 问题的解决办法
- cvs can't commit
- “ERROR: can't get kernel image!”问题的解决方法
- Hibernate的autocommit问题
- SVN commit或import时出现 can't open file 'txn-current-lock' permission denied 的原因及解决方法
- accept()ing when you can’t问题分析
- Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 问题的解决方法
- Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 问题的解决方法
- mysql commit rollback autocommit
- JDBC的autoCommit为true时,其事务管理测试
- Can't connect to local MySQL server through socket '/tmp/mysql.sock'问题的解决方法
- Git之(一)Git是什么
- python 将pyc 文件反编译成py源文件
- R
- (ROS)hector_quadrotor 的编译运行
- 将 ELASTICSEARCH 写入速度优化到极限
- Can't call commit when autocommit=true问题的解决方法
- Redis Cluster集群的搭建与实践
- 5. Longest Palindromic Substring
- Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()
- RedHat6.5 更换Yum源
- OSI模型的下各层及其协议
- 如何在VS2017中单步调试第三方DLL
- spring入门
- 安卓调试stetho抛开繁琐配置——三步搞定