File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Double transaction counts when using Hibernate Statistics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Double transaction counts when using Hibernate Statistics" Watch "Double transaction counts when using Hibernate Statistics" New topic

Double transaction counts when using Hibernate Statistics

James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
I am getting a double count of transactions when using Hibernate Statistics.

The double transaction counting is happening within Hibernate's JDBCContext class. JDBCContext.afterTransactionCompletion() is called after the transaction is completed, which results in an increment of the statistics' transaction count, and then this method is called again in JDBCContext.connectionCleanedUp() as a result of a isTransactionalCallbackRegistered flag not being set, resulting in the second increment of the transaction count statistics value. It seems that the JDBCContext's isTransactionalCallbackRegistered flag is set in the registerSynchronizationIfPossible() method if registering synchronization with a JTA TransactionManager, and because I'm using a HibernateTransactionManager in my test this flag never gets set to true.

In short it appears that the transaction count gets incremented when the transaction completes and then again when the session is cleaned up if you're not using a JTA TransactionManager. I'm not sure if this is the desired behavior or a bug, as I don't understand this code very well.

My test is an extension of Spring's AbstractTransactionalDataSourceSpringContextTests, which opens a single transaction which is rolled back at the completion of the test. I assume that I should see one transaction in the Hibernate statistics summary I display at the end of my test, but instead I see two.

If anyone has seen this before, or knows something about it which would enlighten me then I'd certainly appreciate your response. Thanks in advance!

I agree. Here's the link:
subject: Double transaction counts when using Hibernate Statistics