cze*_*zer 9 session spring multithreading hibernate autocommit
我有主线程产生线程#2,它在主线程中使用相同的hibernate Session.线程#2每隔几分钟就会执行一次"选择1",以保持数据库连接处于活动状态,因为主线程需要长时间运行.一旦主线程完成w /处理,它调用提交但我得到错误:
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
Run Code Online (Sandbox Code Playgroud)
在主线程中,它创建成功提交的内部事务,它只是提交时引发此错误的外部事务.我没有看到什么可以改变自动提交布尔值.在我介绍第二个线程以保持连接活动之前,从未发生过此错误.
尽管我认为您应该认真考虑使用Hibernate的方式,但可以通过relaxAutoCommit在其URL中向JDBC驱动程序添加参数来绕过此问题.
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
Run Code Online (Sandbox Code Playgroud)
资料来源:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
在博客中找到了答案,解决方案引用:
在 jdbc url 中设置属性relaxAutoCommit=true,我们解决了问题。
jdbc:mysql://dbserver/database?rewriteBatchedStatements=true&relaxAutoCommit=true
Run Code Online (Sandbox Code Playgroud)
当然博客是另一种场景,跳过“rewriteBatchedStatements=true”部分即可
| 归档时间: |
|
| 查看次数: |
17710 次 |
| 最近记录: |