zoukankan      html  css  js  c++  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的自动提交处于关闭状态。

  • 相关阅读:
    list浅析
    C#尝试读取或写入受保护的内存。这通常指示其他内存已损坏(catch不起作用)
    浅析C#线程同步事件-WaitHandle
    C#操作xml方法1
    C#简单的操作csv文件
    C#的int类型?,??,~的意思,string类型空值赋值
    将多个exc表格汇总于一个表格中
    C#禁止双击标题栏等操作
    c#泛型
    c#session
  • 原文地址:https://www.cnblogs.com/zdb292034/p/8872092.html
Copyright © 2011-2022 走看看