1.

What is the difference between first level cache and second level cache?

Answer»

Hibernate has 2 cache types. First level and second level cache for which the difference is given below:

First Level CacheSecond Level Cache
This is local to the Session object and cannot be shared between multiple SESSIONS.This cache is maintained at the SessionFactory level and shared among all sessions in Hibernate.
This cache is enabled by default and there is no way to disable it.This is disabled by default, but we can enable it through configuration.
The first level cache is AVAILABLE only until the session is OPEN, once the session is closed, the first level cache is destroyed.The second-level cache is available through the application’s life cycle, it is only destroyed and recreated when an application is restarted.

If an entity or object is loaded by CALLING the get() method then Hibernate first checked the first level cache, if it doesn’t find the object then it goes to the second level cache if CONFIGURED. If the object is not found then it finally goes to the database and returns the object, if there is no corresponding row in the table then it returns null.



Discussion

No Comment Found