Understanding Hibernate Caching

Understanding Hibernate Caching –

Hibernate caching is used to optimize application performance, it minimizes the DB connection cost thus reduces data fetch time. First time it will fetch data from DB a

 

nd load objects then these objects will be stored in cache(like in files), on request from next time onwards it will pick from cache. So it is very faster.

hibernate-caching

Hibernate supports two level of caching –

  • First Level Cache
  • Second Level Cache

Hibernate uses first-level cache by default and you have nothing to do to use first-level cache. Second-level cache is optional.

First Level Cache:
Session object holds the first level cache data. It is enabled by default. The first level cache data will not be available to entire application. Objects will be cached within the same session. If you close the session, all the objects being cached are lost and either persisted or updated in the database.

Second Level Cache:
SessionFactory object holds the second level cache data. The data stored in the second level cache will be available to entire application. But we need to enable it explicitly.

you can use any third-party cache with Hibernate. An org.hibernate.cache.CacheProvider interface is provided, which must be implemented to provide Hibernate with a handle to the cache implementation.

Second Level Cache implementations are provided by different vendors such as:

EHCache
OS Cache
Swarm Cache
JBoss Cache

Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *