Friday, 9 December 2016

Caching in Hibernate: First Level and Second Level Cache in Hibernate



Hibernate provide the lots of other features. In which Cache is very important feature one of them. 

Caching is all about application performance optimization and it sits between your application and the database to avoid the number of database hits as many as possible to give a better performance for performance critical applications.


One of the primary concerns of mappings between a database and our Java application is performance.  This is the common concern of the all guys who working with hibernate and spent the more time in ORM tools for performance-enhancing changes to particular queries and retrievals. Today I want to discuss about some facets of the Hibernate infrastructure that are implemented to handle certain performance concerns - 



  1. The first-level cache -Associated with Session 
  2. The second-level cache -Associated with Session-factory
  3. The query cache(2nd level same associated with session factory)
The first-level cache: The first level cache type is the session cache. The session cache caches object within the current session but this is not enough for long level i.e. session factory scope.Its default feature in hibernate.
The second-level cache: The second-level cache is called 'second-level' because there is already a cache operating for you in Hibernate for the duration you have a session open. A Hibernate Session is a transaction-level cache of persistent data. It is possible to configure a SessionFactory-level cache on a class-by-class and collection-by-collection basis.
                 second-level cache
                     -- Across sessions in an Application
                    


When a program want an object from data base then hibernate first checks for that object in level1(session) cache. If that object is not existed in level1 cache then it checks in second level cache. If not existed in second level cache, then only hibernate goes to database.

Hibernate uses first-level cache by default and you have nothing to do to use first-level cache. Let's go straight to the optional second-level cache. Not all classes benefit from caching, so it's important to be able to disable the second-level cache.

The 'second-level' cache exists as long as the session factory is alive. The second-level cache holds on to the 'data' for all properties and associations (and collections if requested) for individual entities that are marked to be cached. 
In hibernate configuration file (hibernate.cfg.xml)  we wrote the following line.
For Disabling the second level of cache we have to made following change to hibernate configuration file.


  1. <!-- Disable the second-level cache -->  
  2. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>


For Enabling the second level of cache we have to made following change to hibernate configuration file.


  1. <!-- Enable the second-level cache -->  
  2. <property name="cache.use_second_level_cache">true</property>
  3.    <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

1. Cache Strategy using with Annotation as some changes made to the Model class also.


  1. @Cacheable
  2. @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
  3. @Entity
  4. @Table(name="EMPLOYEE")
  5. public class Employee implements Serializable
  6. {
2. Cache Strategy using with Mapping file(.hbm.xml) .

  1. <hibernate-mapping>
  2.    <class name="Employee" table="EMPLOYEE">
  3.      
  4.          This class contains the student detail.
  5.      
  6.       <cache usage="read-only">
  7.       <id column="ID" name="id" type="java.lang.Long">
  8.          <generator class="native">
  9.       </generator></id>
  10.       <property column="Employee_NAME" name="employeeName" type="java.lang.String">
  11.       <property column="ADDRESS" name="address" type="java.lang.String">
  12.       <property column="Employee_Code" name="emp_code" type="java.lang.Long">
  13.    </property></property></property></cache></class>
  14. </hibernate-mapping>