Software: Hibernate 3.1.2, MySQL 5 (MySQLMyISAMDialect), MySQL JDBC 5
I'm trying to get familiar with transaction handling using Hibernate. In particular I'm trying to run the "GenerateDeadlock" example in chapter 9 of "Pro Hibernate3" published by Apress (
www.apress.com has the source code). That example consists of 2 threads each of which is trying to update the same 2 tables, but in opposite order so as to create deadlock because the 2 updates each thread performs are part of the same transaction. Pretty basic stuff. Each thread task is coded to catch a HibernateException upon deadlock at which time an explicit rollback is performed. This should result in both tables containing the same value as only one transaction will succeed. However, no HibernateException is ever caught! In fact no exception of any kind is being thrown by the run method of the Task body (I added a catch for Throwable). I am completely baffled by this. The authors of the code apparently thought their code was correct, and having spent a lot of time with the documentation I have found nothing that might suggest they're wrong. It gets worse. Not only is no exception being thrown, but the transaction as delineated by Hibernate is not being honored. Most of the time the 2 units of work in each thread transaction (called step1 & step2 in the code) are being treated as individual transactions which is totally contrary to expectation. Sometimes the two tables contain the same value suggesting that only one transaction succeeded, but as I mentioned previously this is happening without a HibernateException and explicit rollback. The fact that the results are not the same on all runs of GenerateDeadlock makes this even more serious, it basically means these software components are unusable in combination. It may be that the JDBC driver or MySQL is munging something up, but shouldn't Hibernate be bubbling up some kind of status indication whether it be a Hibernate exception or a rethrow of an exception caught by Hibernate? Any suggestions/ideas on how to get a handle on this are most welcome.