I am using hibernate 4.2.12.FINAL
I found that the OneToManyPersister is often doing an unnecessary count query which can have a large negative affect on performance in some circumstances and is easily fixed.
The method doProcessQueuedOps is performing a count query any time an addition or deletion occurs in the collection. But it is only necessary when:
Code:
isInverse && hasIndex && !indexContainsFormula
This can cause contention when many threads are writing new objects to the associated table.
To fix this the writeIndex method could be wrapped in an if statement.
Code:
protected void doProcessQueuedOps(PersistentCollection collection, Serializable id, SessionImplementor session)
throws HibernateException {
if ( isInverse && hasIndex && !indexContainsFormula ) {
writeIndex( collection, collection.queuedAdditionIterator(), id, getSize( id, session ), session );
}
}
I am not sure if this issue affects newer versions of hibernate but it still happens in 4.2.13.final.