We're updating the issue view to help you get more done. 

Hibernate triggers unnecessary select count query that imposes negative effect on performance.

Description

org.hibernate.persister.collection.AbstractCollectionPersister.java:

public void processQueuedOps(PersistentCollection collection, Serializable key, SessionImplementor session)
throws HibernateException {
if ( collection.hasQueuedOperations() ) {
int nextIndex = getSize( key, session );
doProcessQueuedOps( collection, key, nextIndex, session );
}
}

Please note the line:
int nextIndex = getSize( key, session );

It actually executes a query "select count xxx" which is expensive and has negative impact on performance.

More importantly, in the method doProcessQueuedOps:

@Deprecated
protected void doProcessQueuedOps(PersistentCollection collection, Serializable key,
int nextIndex, SessionImplementor session)
throws HibernateException {
doProcessQueuedOps( collection, key, session );
}

The param nextIndex is not even used.

It appears that the method
doProcessQueuedOps(PersistentCollection collection, Serializable key,
int nextIndex, SessionImplementor session)
is already deprecated and is not supposed to be used any more.

The method processQueuedOps should call
doProcessQueuedOps(PersistentCollection collection, Serializable key, SessionImplementor session)
and no need to invoke getSize method that is very expensive.

In addition, in org.hibernate.persister.collection.OneToManyPersister.java,
the following method also triggers unnecessary call getSize() that executes "select count.." query:

protected void doProcessQueuedOps(PersistentCollection collection, Serializable id, SessionImplementor session)
throws HibernateException {
writeIndex( collection, collection.queuedAdditionIterator(), id, getSize( id, session ), session );

}

Environment

None

Status

Assignee

Gail Badner

Reporter

Gary Hu

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Priority

Major