Sunday, 14 April 2019

How to configure Hibernate Second Level Cache using EHCache?


EHCache is the best choice for utilizing hibernate second level cache. Following steps are required to enable EHCache in hibernate application.

Add hibernate-ehcache dependency in your maven project, if it’s not maven then add corresponding jars.



  1.   
  2.   
  3.         org.hibernate  
  4.   
  5.         hibernate-ehcache  
  6.   
  7.         4.3.5.Final  

  •   
  •   

  • Add below properties in hibernate configuration file.

    1. "hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory  
    2.   
    3.            
    4.   
    5.   
    6.   
    7.   
    8.   
    9.             
    10.   
    11.   
    12.   
    13. "hibernate.cache.use_second_level_cache">true
      
  •   
  • "hibernate.cache.use_query_cache">true
  •   
  •   
  • "net.sf.ehcache.configurationResourceName">/myehcache.xml

  • Create EHCache configuration file, a sample file myehcache.xml would look like below.

    1. "1.0" encoding="UTF-8"?>  
    2.   
    3. "http://www.w3.org/2001/XMLSchema-instance"  

  •   
  •     xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  
  •   
  •     monitoring="autodetect" dynamicConfig="true">  
  •   
  •    
  •   
  •     "java.io.tmpdir/ehcache" />  
  •   
  •    
  •   
  •     "10000" eternal="false"  
  •   
  •         timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"  
  •   
  •         maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"  
  •   
  •         memoryStoreEvictionPolicy="LRU" statistics="true">  
  •   
  •         "localTempSwap" />  
  •   
  •     
  •   
  •   
  •    
  •   
  •     "employee" maxEntriesLocalHeap="10000" eternal="false"  
  •   
  •         timeToIdleSeconds="5" timeToLiveSeconds="10">  
  •   
  •         "localTempSwap" />  
  •   
  •     
  •   
  •   
  •    
  •   
  •     "org.hibernate.cache.internal.StandardQueryCache"  
  •   
  •         maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120">  
  •   
  •         "localTempSwap" />  
  •   
  •     
  •   
  •   
  •    
  •   
  •     "org.hibernate.cache.spi.UpdateTimestampsCache"  
  •   
  •         maxEntriesLocalHeap="5000" eternal="true">  
  •   
  •         "localTempSwap" />  
  •   
  •     
  •   
  •   

  • Annotate entity beans with @Cache annotation and caching strategy to use. For example,
    Copy

    1. import org.hibernate.annotations.Cache;  
    2.   
    3. import org.hibernate.annotations.CacheConcurrencyStrategy;  
    4.   
    5.   
    6.   
    7. @Entity  
    8.   
    9. @Table(name = "ADDRESS")  
    10.   
    11. @Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region="employee")  
    12.   
    13. public class Address {  
    14.   
    15.   
    16.   
    17. }  

    No comments:

    Post a Comment

    Spring Boot @ConfigurationProperties and Properties File

     In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...