【问题标题】:JDBC commit failed, calling commit when autocommit=true. Multithreaded hibernate session somehow changing autocommit?JDBC 提交失败,当 autocommit=true 时调用 commit。多线程休眠会话以某种方式改变自动提交?
【发布时间】:2011-07-12 20:24:36
【问题描述】:

我的主线程产生线程#2,它在主线程中使用相同的休眠会话。线程 #2 每隔几分钟就会执行一次“选择 1”以保持数据库连接处于活动状态,因为主线程的进程运行时间很长。主线程完成处理后,它会调用提交,但我收到错误:

Caused by: org.hibernate.TransactionException: JDBC commit failed
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:161)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
    ... 5 more
Caused by: 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:1602)
    at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:170)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)
    ... 6 more

在主线程中,它创建成功提交的内部事务,它只是在提交时抛出此错误的外部事务。我看不出有什么可以改变自动提交布尔值。在我引入第二个线程来保持连接活跃之前,这个错误从未发生过。

【问题讨论】:

  • 您是否同时使用来自多个线程的单个连接?
  • 是的,没错。线程#1 创建打开数据库连接的会话,线程#2 使用相同的会话和连接。线程 #1 将在该连接中创建新事务以执行更新,而线程 #2 仅从数据库中执行 select 1 以保持连接处于活动状态。

标签: multithreading hibernate spring session autocommit


【解决方案1】:

尽管我认为您应该认真重新考虑使用 Hibernate 的方式,但您可以通过在其 URL 中向 JDBC 驱动程序添加 relaxAutoCommit 参数来绕过此问题。

来自 MySQL 文档的详细信息:

relaxAutoCommit

If the version of MySQL the driver connects to does not support transactions, still allow calls to commit(), rollback() and setAutoCommit() (true/false, defaults to 'false')?

Default: false

Since version: 2.0.13

来源:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html

【讨论】:

    【解决方案2】:

    blog找到答案,解决方案引用:

    在 jdbc url 中设置属性relaxAutoCommit=true 解决了我们的问题。

    jdbc:mysql://dbserver/database?rewriteBatchedStatements=true&relaxAutoCommit=true
    

    当然博客是另一种场景,直接跳过“rewriteBatchedStatements=true”部分

    【讨论】:

      猜你喜欢
      • 2011-03-23
      • 2010-11-16
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2021-03-07
      • 2019-09-22
      相关资源
      最近更新 更多