Caching in Hibernate
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.
Caching is important to Hibernate as well which utilizes a multilevel caching schemes as explained below:
- The first-level cache – Session (Earlier hibernate already provide this level of cache)
- The second-level cache –Session-factory-level cache
- and the query cache.
The first-level cache:
The second-level cache:
— Across sessions in an Application
— Across applications (different applications on same servers with same database)
— Across clusters (different applications on different servers with same 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.
For Disabling the second level of cache we have to made following change to hibernate configuration file.
<!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Enable the second-level cache --> <property name="cache.use_second_level_cache">true</property> <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
@Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) @Entity @Table(name="STUDENT") public class Student implements Serializable { ....
<hibernate-mapping> <class name="Student" table="STUDENT"> This class contains the student detail. <cache usage="read-only"> <id column="ID" name="id" type="java.lang.Long"> <generator class="native"> </generator></id> <property column="STUDENT_NAME" name="studentName" type="java.lang.String"> <property column="lCOURSE" name="course" type="java.lang.String"> <property column="ROLL_NUMBER" name="rollNumber" type="java.lang.Long"> </property></property></property></cache></class> </hibernate-mapping>
Concurrency strategies:
- Transactional: Use this strategy for read-mostly data where it is critical to prevent stale data in concurrent transactions,in the rare case of an update.
- Read-write: Again use this strategy for read-mostly data where it is critical to prevent stale data in concurrent transactions,in the rare case of an update.
- Nonstrict-read-write: This strategy makes no guarantee of consistency between the cache and the database. Use this strategy if data hardly ever changes and a small likelihood of stale data is not of critical concern.
- Read-only: A concurrency strategy suitable for data which never changes. Use it for reference data only.
HibernateTestDemo.java
package com.sdnext.hibernate.tutorial; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.sdnext.hibernate.tutorial.dto.Student; public class HibernateTestDemo { /** * @param args */ public static void main(String[] args) { SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); Student student = (Student) session.get(Student.class, 1); session.getTransaction().commit(); session.close(); Session session2 = sessionFactory.openSession(); session2.beginTransaction(); Student student2 = (Student) session2.get(Student.class, 1); session2.getTransaction().commit(); session2.close(); } }
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
Hibernate: select student0_.ID as ID0_0_, student0_.COURSE as COURSE0_0_, student0_.ROLL_NUMBER as ROLL3_0_0_, student0_.STUDENT_NAME as STUDENT4_0_0_ from STUDENT student0_ where student0_.ID=?
In the Next Chapter we will discuss more about the Query Caching in Hibernate.
<<Previous Chapter 31<< >>Next Chapter 33>>