Hibernate 2nd level cache with HazelCast, Spring Boot and Spring Data JPA

This blog explains the process to implement Hibernate 2nd level cache with HazelCast, Spring Boot and Spring Data JPA. Source code and config files are uploaded to Github

Technologies

  1. Spring Boot 2.2.x
  2. Spring Data JPA 2.2.x
  3. HazelCast 4.0
  4. Docker

Start HazelCast instance

HazelCast instances are setup as Docker containers using the docker-compose file shown below.

Start the HazelCast instances and management center using the following command.

$ docker-compose -f <location of the docker-hazelcast.yml > up

HazelCast management center should up and running at http://localhost:8090/login.htmlCreate a new account to see the management dashboard.

Hazelcast default Dashboard

Click on Add Cluster Config to add newly created cluster config. Enter the cluster name as dev(default name) and replace 192.168.1.157 with your host IP Address as shown below.

Add dev cluster config

Save the config and go to home page to see list of added clusters. Select dev cluster and you should see the following dashboard

Dev Dashboard

Spring Boot Application

  1. HazelCastConfig class defines Config bean needed to connect Spring Boot app to HazelCast cluster as shown below

2. The demo Spring Boot application has Employee domain object with EmployeeController and EmployeeRepository defined using Spring Data JPA. It uses H2 in memory data base to persist data

@Data
@Entity
@Cache(region = “employeeCache”, usage = CacheConcurrencyStrategy.READ_WRITE)
public class Employee
{
@Id
private String empId;
private String empName;
}

3. The annotation @Cache creates employeeCache map in HazelCast cache and takes care of CachePut and CacheEvict operations. By default findAll(), create, update, delete method calls hit database and findById() method hits HazelCast cache.

4. Run the application and go to http://localhost:8080/api/v1/employee/find/all to see all the employees.

5. Create new employee by hitting the link http://localhost:8080/api/v1/employee/create

Hibernate Logs:
Hibernate: select employee0_.emp_id as emp_id1_0_0_, employee0_.emp_name as emp_name2_0_0_ from employee employee0_ where employee0_.emp_id=?Hibernate: insert into employee (emp_name, emp_id) values (?, ?)Hibernate: select employee0_.emp_id as emp_id1_0_0_, employee0_.emp_name as emp_name2_0_0_ from employee employee0_ where employee0_.emp_id=?

6. If you look at hibernate logs, you should see 2 select statements for 2 findAll() method calls and 1 insert statement for findById() method call

5. Now try to get employee information by providing the ID created in step 2. http://localhost:8080/api/v1/employee/find/<your_emp_id>. If you look at the hibernate logs, you should see nothing. That means Spring Boot returned the employee record from HazelCast cache without hitting the database.

7. Look at the following employeeCache map from HazelCast management center, you should be see all the Gets and Puts to cache


Source code is available in Github for reference and Happy Coding

Pavan Kumar Jadda
Pavan Kumar Jadda
Articles: 36

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.