JDBC commit failed, calling commit when autocommit=true. Multithreaded hibernate session somehow changing autocommit?
Asked Answered
V

2

9

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.

Virile answered 10/3, 2011 at 16:4 Comment(2)
Are you using a single connection from multiple threads at once?Denouement
Yes, that's correct. Thread #1 creates the sessions which opens the db connection, and Thread #2 uses that same session and connection. Thread #1 would create new transactions within that connection to perform updates, while thread #2 only does a select 1 from the DB to keep the connection alive.Virile
M
6

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

Masinissa answered 10/3, 2011 at 16:46 Comment(0)
V
5

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

Vibratile answered 15/4, 2014 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.