Hibernate Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed.

Cache provider

The following are the popular cache providers:

JBoss Cache

It is a transactional and replicated cache. It contains two components, a generic cache, and a POJO cache. It also supports synchronous/asynchronous communication, an optimistic and pessimistic locking. It is available in org.hibernate.cache.TreeCacheProvider package.

EH Cache

EH Cache is a famous open-source Java cache which can be used as a Hibernate second-level cache. Also, it can be used as a standalone second-level cache. It is available in org.hibernate.cache.EhCacheProvider package.

OS Cache

OS Cache is developed by OpenSymphony, which is a Java framework. It can be configured to behave as a second-level cache. It is available in org.hibernate.cache.OSCacheProvider package.

Swarm Cache

Swarm cache is a simple, distributed cache. It internally uses JavaGroups to manage the membership and communications of its distributed cache. It doesn’t support Hibernate query cache. It is available in org.hibernate.cache.SwarmCacheProvider package.

Hashtable

It is not intended for production use. It is only used for testing purpose and available in org.hibernate.cache.HashtableCacheprovider package.   

Concurrency Strategy of Second-level Cache

They act as a mediator, which are responsible for storing data into the cache and fetching data from the cache. There are five concurrency strategies which a user uses when he/she enable the second-level cache. The following are the concurrency strategies:

  1. Read-only
  2. Read-write
  3. Nonstrict read-write
  4. Transactional
  5. None

Read-only

A read-only cache strategy is used for the data which never wants to be changed. It is suitable for the data that needs to be often read but not modified. It is safe and straightforward to use in a clustered environment.

Read-Write

A read-write cache strategy is suitable for an application which needs to be updated regularly. It should never be used if serializable transaction isolation is required. If we want to use this strategy in a cluster, we should check whether the cache implementation supports locking or not.

Nonstrict read-write

If an application is rarely updated (i.e., it is not updated regularly), a nonstrict read-write cache strategy is used. In this strategy, there is no guarantee of consistency between the cache and the database.

Transactional

It provides support for transactional cache providers like JBoss TreeCache. This caching strategy can only be used in a JTA environment, and we must specify a hibernate.transaction.manager_lookup_class.

None

In this strategy, no caching will happen.

Cache concurrency strategy support table

Not all of the cache providers support all cache concurrency strategies. The following table shows which cache provider is compatible with which cache usage functionality.

  Cache Read-only Read-write Nonstrict read-write Transactional
EH Cache          Yes           Yes          Yes           No
OS Cache          Yes           Yes          Yes           No
JBoss Cache          Yes            No           No           Yes
Swarm Cache          Yes            No          Yes           No
Hashtable          Yes           Yes          Yes           No

Example of Second-Level Cache

Let us create an example of the second-level cache. In this example, we are using EH Cache provider to act as a second-level cache. We are assuming that there is an existing table (students) in the database containing some records.

Following are the steps to create an example of Second-level Cache:

  1. Create the POJO class

In this step, we are going to create a POJO class, i.e., Student .java.

Student.java

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name="students")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Sid")
private int id;
@Column(name="S_name")
private String name;
@Column(name="S_rno")
private int rollno;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
} 
  • Add dependencies

In this step, we will add dependencies which are required by the project into pom.xml between the <dependencies>…</dependencies> tag.

 
net.sf.ehcache
ehcache
2.9.1


org.hibernate
hibernate-ehcache
5.4.0.Final
 
  • Create the configuration file.

The configuration file contains the information of mapping class and database.

hibernate.cfg.xml

   
  
  
  
update  
org.hibernate.
dialect.MySQL5Dialect
jdbc:mysql://
localhost:3306/test2
com.mysql.
jdbc.Driver
root  
root   
true
true
           
org.hibernate.
cache.ehcache.EhCacheRegionFactory

  
   
  • Create a class that retrieves the POJO object

It is a class which contains the main() methodused to run the application, and it retrieves the object of the POJO class.

App.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class App 
{
public static void main( String[] args )
{
Configuration cfg= new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory fact= cfg.buildSessionFactory();
Session sess= fact.openSession();
sess.beginTransaction();
Student std=  (Student) sess.get(Student.class, 2);   
sess.getTransaction().commit();
sess.close();
System.out.println(std);
//opening a new session
Session sess2= fact.openSession();
sess2.beginTransaction();
Student student=(Student) sess2.get(Student.class, 2);
sess2.getTransaction().commit();
sess2.close();
System.out.println(student);
}
} 

OUTPUT

Hibernate Second-Level Cache