JDBC 提交失败,当 autocommit=true 时调用提交。多线程休眠会话以某种方式改变自动提交?
我有一个生成线程 #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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管我认为您应该认真重新考虑使用 Hibernate 的方式,但您可以通过在 JDBC 驱动程序的 URL 中添加一个
relaxAutoCommit
参数来绕过这个问题。MySQL 文档的详细信息:
来源: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:
Source: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
在 博客 中找到了答案,解决方案引用了:
当然博客是另一种场景,跳过“rewriteBatchedStatements=true”部分即可
found the answer in a blog, the solution quotes:
Of course the blog is in another scenario, just skip the "rewriteBatchedStatements=true" part