I recently came across a disturbing use case, could someone explain to me what’s happening ?
- i start a transaction, isolation =default (read commited, i’m using postgres)
- i fetch an entity from my database with : Book book = entityManager.createQuery(“from Book where name=‘A’”, Book.class).getSingleResult();
- i pause the current thread, and update the book in my database to set the name ‘B’ instead of ‘A’ in another thread / commited transaction
- back to my first thread, i fetch again the entity from my database but using this query : Book book2 = entityManager.createQuery(“from Book where name=‘B’”, Book.class).getSingleResult();
- A request to the database is issued, and it indeed return a book BUT the resulting entity book2 is NOT updated, its name is still ‘A’
Any idea why hibernate won’t update his entity in his session even after retrieving it from the database ? Even if this is a read commited isolation, to me the ending result looks like…if i were in a repeatable read isolation situation !
Thanks for your help