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.


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


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.


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.


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.


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.


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;
public class Student {
private int id;
private String name;
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.


  • Create the configuration file.

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



  • 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.


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();
SessionFactory fact= cfg.buildSessionFactory();
Session sess= fact.openSession();
Student std=  (Student) sess.get(Student.class, 2);   
//opening a new session
Session sess2= fact.openSession();
Student student=(Student) sess2.get(Student.class, 2);


Hibernate Second-Level Cache