Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable second level cache in Hibernate

I need some pojo objects across my application so I want to know how to enable Second Level Cache. Until now by default First Level cache is enabled, I would also like to know what advantages and disadvantages of Second Level cache there are.

like image 274
Anirudh Jadhav Avatar asked Dec 08 '14 12:12

Anirudh Jadhav


Video Answer


1 Answers

This is what you need to do:

  1. Set the following Hibernate properties:

     <property name="hibernate.cache.use_second_level_cache">true</property>
     <property name="hibernate.cache.provider_class">ehcache</property>
    
  2. Add an ehcache.xml file in your classpath, containing the cache configuration entries:

     <cache name="com.mycompany.MyEntity"
        maxElementsInMemory="50"
        eternal="true"
        overflowToDisk="false"
        timeToIdleSeconds="600"
        timeToLiveSeconds="600"
        diskPersistent="false"
        memoryStoreEvictionPolicy="LRU"       
     />
    
  3. Define the Caching type for each entity:

     @Entity
     @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
     public class MyEntity {
             ...
     }
    
like image 163
Vlad Mihalcea Avatar answered Oct 04 '22 18:10

Vlad Mihalcea