DEV Community

Pavan K Jadda
Pavan K Jadda

Posted on

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.

docker-hazelcast.yml

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

$ docker-compose -f <location of the docker-hazelcast.yml > up
Enter fullscreen mode Exit fullscreen mode

HazelCast management center should up and running at http://localhost:8090/login.html. Create 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

HazelCastConfig.java

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;  
}
Enter fullscreen mode Exit fullscreen mode

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=?  

Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)