Caching In Hibernate

Posted By : Rahul Jha | 19-Jan-2015

Cache/Buffer is a temprory memory that holds result or data and uses that data or result across the multiple same request given by application , this process is called Caching or Buffering.

In client-Server application , if caching is enabled at client side, it reduces network round trip between client and server applications.

The Caching or Buffering done in Hibernate Based application , stores result collected from the Db s/w in the form of Hibernate pojo class object, this reduces Network Round Trip between Hibernate based client application and DB s/w across the multiple same request given by Hibenate Based client application.

Hibernate support two level of caching
    1. 1st level caching/ L1 Caching.
    2. 2nd level caching/ L2 Caching.

1st Level Cache:

  • It is Built in cache.
  • Every session object contains 1st level cache by default.

Usefull Method for control over 1st level cache:

  •  session.evict(pojo object) :
     It removes given pojo object from Ist  level cache

  • session.clear()  :
     It Removes all Hibernate pojo class from Ist level cache. 

2nd Level Cache : 

  • 2nd level cache is configurable cache.
  • 2nd level cache  will be enabled only when programer configures it manually.
  • 2nd level cache is also called as Global Cache because we can store and manage the result generated by all the Hibernate Session object.
  • There are multiple vendor supplying s/w to enable 2nd level cache in hibernate application e.g. Ehcache,OSCache,TreeCache,SwarmCache and etc.
  • Most of time developer prefer with Ehcache because it is fast , light and easy to use. Ehcache related jar files come along with HB s/w installation.

Caching Process:

Procedure to enable 'Ehcache' as second level cache in hibernate Application :

Step1. Configure Ehcache provided class name in Hibernate configuration file.


  ------
  ------
  org.hibernate.cache.EhcacheProvider

Step2. Add Ehcache related jar files to the classpath.

Step3. Enable second level cache in Hibernate Application in Hibernate configuration file.

 

true


Step4. write Ehcache.xml file long with other resources of Hibernate application, specifying the parameter related to Ehcache.

ehcache.xml
===========

   
   




Query Cache:

  • Query cache is a part of Second level cache.Basically it stores HQL, NativeSql queries generated result.

Procedure for enable Query Level Cache in Hibernate Application:


Step1. Enable Query Level Cache from Hibernate Configuration File.

true


Step2. Make HQL, NativeSql Query result as cachable from client application.

Query q1=session.createQuery("from < pojo name="" > ") ;                           
q1.setCacheable(true);                               
-------                             
-------                              
------- 

NOTE: Make Sure that second level cache also enable before the QueryCache.

Methods for control over second level cache :

  • sessionfactory.close() :

          Release Second level cache.

  • sessionfactory.evict(<pojoname.class>)

       Remove all object of given pojo class from second level cache.

  •    sessionfactory.evictCollection("phones")

      If phones property configured in Hibernate Mapping File , is related to collection f/w data structure, all collection f/w DS objects representing phones property will be  removed from second level cache.

  • sessionfactory.evictQueries("test")

          Removes HQL, NativeSql queries from the Named Query Cached Region  called "test".(where "test" is the logical name of Query cached Region)

 

Thanks.

 

About Author

Author Image
Rahul Jha

Rahul is an expert Java developer having experience in frameworks like spring and hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..