JDBC 提交失败,当 autocommit=true 时调用提交。多线程休眠会话以某种方式改变自动提交?

发布于 2024-10-21 20:50:41 字数 927 浏览 2 评论 0原文

我有一个生成线程 #2 的主线程,它在主线程中使用相同的休眠会话。由于主线程的进程运行时间较长,线程 #2 每隔几分钟执行一次“select 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

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

I have the main thread that spawns thread #2 which uses the same hibernate Session in the main thread. Thread #2 just does a "select 1" every few min to keep the db connection alive because of a long running process from the main thread. Once the main thread is done w/ the processing, it calls a commit but i get the error:

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

Within the main thread, it creates inner transactions which are committed successfully, it's just the outer transaction when it commits that throws this error. I don't see what could be changing the autocommit boolean. Before I introduced the 2nd thread to keep the connection alive, this error had never occurred.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2024-10-28 20:50:41

尽管我认为您应该认真重新考虑使用 Hibernate 的方式,但您可以通过在 JDBC 驱动程序的 URL 中添加一个 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

Even though I think you should seriously reconsider the way you are using Hibernate, you can bypass this issue by adding a relaxAutoCommit parameter to the JDBC driver in its URL.

Details from MySQL documentation:

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

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

ˉ厌 2024-10-28 20:50:41

博客 中找到了答案,解决方案引用了:

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

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

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

found the answer in a blog, the solution quotes:

Setting the attribute relaxAutoCommit=true in the jdbc url we solved our problem.

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

Of course the blog is in another scenario, just skip the "rewriteBatchedStatements=true" part

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文