After some debugging, I found the reason for the multiple onFlushDirty calls:
As explained above, the flash mode in my application is set to AUTO. Before a request is executed inside a transaction, Hibernate decides whether to hide dirty objects or not. Whatever the outcome of this decision: the onFlushDirty method in the HibernateInterceptor is called for sure for every dirty object in the session, even if it decided not to reset later.
If an object of type A is in a dirty state in a session, and a query for entity type B is executed in this session, Hibernate does not clear the dirty object, because it does not affect the table that the query uses, and therefore a flash is not needed.
If several requests are executed in a session, it may happen that the onFlushDirty method is called several times for the same dirty object, but it never spoils into the database and remains dirty until the current transaction is completed.
Take a look at the Hibernate DefaultAutoFlushEventListener class
public void onAutoFlush(AutoFlushEvent event) throws HibernateException { ... if ( flushIsReallyNeeded(event, source) ) { log.trace("Need to execute flush"); ... } else { log.trace("Dont need to execute flush"); ... } }
theFriedC
source share