0

I'm working with SpringBoot 2.3.5 with Hibernate and Hikari Pool.

I've an entity, lets call it A, this entity has an "execution status counter" long field, incremented by an Async method, at the end of its executions, so I can split the execution on multiple threads, having a progress counter.

The increment is performed like this:

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Lock(LockModeType.PESSIMISTIC_WRITE)
default void incrementStatusCount(String lotto, Long progressivo) {
    A a = findById(...).get();
    a.setStatoLdSN(a.getStatoLdSN() != null ? a.getStatoLdSN()+1:1L);
    saveAndFlush(a);
}

And it works fine, so externally, using a DB Tool, I can see the counter update. Now, at the end of my whole execution, I change the string status of my whole execution, so I load the entity, but the entity is already in the cache, so I call a refresh on the entity manager, I can see the query performad and also the retrieved values from the Hibernate log....and my counter 0. Now, Oracle default isolation leve is READ_COMMITTED (and I verified it on the connection), and my incremented value is committed because I saw it using the DB client, no? So why JPA, not even calling refresh is loading the right value?

user5919369
  • 101
  • 6
  • refresh isn't what you think, see https://stackoverflow.com/questions/5832415/entitymanager-refresh – Turo Jan 15 '21 at 08:43
  • I do not agree, refresh must update mine object with the database, in fact...it cause a query in my log, a query that don't load the correct value, maybe there is some query cache – user5919369 Jan 15 '21 at 09:46
  • Could you post a *reproducible* example or at least some more hints how do you loose the counter? – Marmite Bomber Jan 16 '21 at 10:57

0 Answers0