Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate first level cache - does it Sync?

I am aware of the fact that Session is first level cache used by Hibernate, and once we retrieve an entity from the session, the subsequent get calls for the same entity with same identifier is fetched from the session instead of DB, until the session is Open.

Having said that, I have a doubt regarding how hibernate syncs the first level cache with DB? Consider the following scenario

//Lets say I have created the session

Session s1 = sessionFactory.getSession();
User u1 = s1.get(User.class, 1); //Getting User with ID=1
//s1 is not yet closed

//Lets say I create some other session

Session s2 = sessionFactory.getSession();
User u2 = s2.get(User.class, 1); //Getting User with ID=1
u2.setName("Abc"); // Changed a field
s2.save(u2); // Saved the changes to DB
s2.close(); //Closed the 2nd session

//Now when I once again retrieve User with ID=1 from s1, will I get updated User?
User u3 = s1.get(User.class, 1);// Here as per my understanding cache is used

So my question is

  • Since u3 is fetched from 1st level cache, does u3 have updated value?
  • If some one directly updates DB and modifies User object when session is open, does the session sync with DB?

Thanks in advance for your time and effort on this thread

like image 991
sanbhat Avatar asked Sep 10 '13 14:09

sanbhat


People also ask

How does Hibernate first level cache work?

The first-level cache is associated with a specific “session” object and other session objects in the application can not see it. The scope of cache objects is of the session. Once the session is closed, cached objects are gone forever. The first-level cache is enabled by default and we can not disable it.

What is the difference between L1 and L2 cache Hibernate?

L1:The first-level cache is the cache per Hibernate Session cache and is a mandatory cache through which all requests must pass and this cache is not shared among threads. L2:The second-level cache can be configured on a per-class and per-collection basis and mainly responsible for caching objects across sessions.

Which of the following is true about first level cache in Hibernate?

Q 14 - Whis of the following is true about first level cache in hibernate? A - The first-level cache is the Session cache.

What is caching in Hibernate difference between 1st level and 2nd level caching?

The main difference between the first level and second level cache in Hibernate is that the first level is maintained at the Session level and accessible only to the Session, while the second level cache is maintained at the SessionFactory level and available to all Sessions.


2 Answers

No, Hibernate doesn't do anything to synchronize state of the entities in session cache with the DB, unless you request it explicitly.

Usually it's not a problem, because active work usually happens inside a transaction, and operations inside a transaction are not supposed to see changes made by other concurrent transactions (details depend on isolation level, though). So, behavior of Hibernate complements the typical semantics of transaction isolation in this case.

There can also be situations when state of the entity need to be explicitly synchronized to reflect changes made inside the same transaction. It may be caused by bulk update queries or execution of database triggers. In this case you need to request such a synchronization explicitly by calling refresh().

like image 148
axtavt Avatar answered Sep 25 '22 02:09

axtavt


You'll need to call Session.evict(Object) on s1 with u1 as an argument in order to get a fresh lookup. Alternatively, for this case, you could also call Session.clear() on s1 as well to get the same effect. You could also call Session.refresh(Object object) to just refresh the state of u1 as well.

Note that transactions can take an undefined amount of time (usually not very long though) to be seen by other clients after they are committed. The update may not be visible immediately to other sessions using different connections.

like image 36
Dev Avatar answered Sep 23 '22 02:09

Dev