20 January 2021

Hibernate cache

First level cache

  • Its associated with Session
  • Its enabled by default and it can’t be disabled
  • Hibernate provides a way to delete or clear the object completely from cache
  • Object cached in session can’t be visible in any other session
  • When the session is closed all the related object in the cache will be lost
  • clear example to remove everything from first level cache session.clear();
  • Evict the employee object session.evict(emp);
  • We can use session evict() method to remove a single object from the hibernate first level cache.
  • We can use session clear() method to clear the cache i.e delete all the objects from the cache.
  • We can use session contains() method to check if an object is present in the hibernate cache or not, if the object is found in cache, it returns true or else it returns false.

Second level cache

  • This is used for caching object at session factory
  • This is disabled by default it can be enabled through configuration
  • EHCache and Infinispan provides the implementation for the second level cache
  • Add ehcache-core and hibernate-ehcache dependencies 
<!-- enable second level cache and query cache -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>        
<property name="net.sf.ehcache.configurationResourceName">/myehcache.xml</property>
  • hibernate.cache.region.factory_class is used to define the Factory class for Second level caching. If you want the factory class to be singleton, you should use org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory class.
  • If you are using Hibernate 3, corresponding classes will be net.sf.ehcache.hibernate.EhCacheRegionFactory and net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory.
  • hibernate.cache.use_second_level_cache is used to enable the second level cache.
  • hibernate.cache.use_query_cache is used to enable the query cache, without it HQL queries results will not be cached.
  • net.sf.ehcache.configurationResourceName is used to define the EHCache configuration file location, it’s an optional parameter and if it’s not present EHCache will try to locate ehcache.xml file in the application classpath.
  • diskStore: EHCache stores data into memory but when it starts overflowing, it start writing data into file system. We use this property to define the location where EHCache will write the overflown data.
  • Statistics stats = sessionFactory.getStatistics(); org.hibernate.stat.Statistics provides the statistics of Hibernate SessionFactory
  • EHCache is the best choice for utilizing hibernate second level cache.
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
 
<!-- For singleton factory -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
 
<!-- enable second level cache and query cache -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="net.sf.ehcache.configurationResourceName">/myehcache.xml</property>

Query cache

  • Hibernate can also cache result set of a query.
  • Hibernate Query Cache doesn’t cache the state of the actual entities in the cache; it caches only identifier values and results of value type. So it should always be used in conjunction with the second-level cache.

No comments:

Post a Comment

Most views on this month