I'm trying to improve the performances of my asynk transactional method.
In this task I have to read almost 7500 record from a table, elaborate it, and insert/update a corresponding row in another table.
I'm using spring data jpa with hibernate.
In order to get a
Session session = (Session) em.unwrap(Session.class);
ScrollableResults res = session.createQuery("from SourceTable s")
.... // em.flush() called every 40 cycles
query = em.createQuery("from DestTable d where d.item.id = :id", DestTable.class);
The problem is that the default flush mode is
FlushModeType.AUTO. In auto flush mode, Hibernate will flush before each query (only queries, not find operations). This means that in your above example, by default, Hibernate is flushing each time you call
getSingleResult(). The reason it does this is because it's possible that the changes you have made would affect the results of your query and so Hibernate wants your query to be as accurate as possible and flushes first.
You don't see the performance hit in your first example because you are only issuing one query and scrolling through it. The best solution I have found is the one you mentioned which is just to set the flush mode to
COMMIT. There should be no difference between calling
setFlushMode on the
Session or the