Difference Between First level cache and Second level cache in Hibernate


Hibernate support two type of cache one is first level cache and other is second level cache. 

First level cache is a session level cache and it is always associated with session level object. This type of cache is used for minimizing Db interaction by caching the state of the object. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.

Second level cache is session factory level cache and it is available across all sessions.While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will be available to the entire application, not bound to a single user. By default, it is disabled in hibernate. 

Sr. No.KeyFirst level cacheSecond level cache

1

Basic 

First level cache is a session level cache and it is always associated with session level object

Second level cache is session factory level cache and it is available across all sessions

2

Enabled 

It is enabled by default.


It is not enabled by default.

3


Availability 

It is available for a session 

It is available across all session.

4

Configuration 

No Extra configuration required 

We have to decide  which concurrency strategy to use and also need to configure cache expiration and physical cache attributes.


Updated on: 21-Jan-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements