Getting Started with Hazelcast and Hibernate Second-Level Cache

See something wrong? Edit this page.

This guide will get you started to use Hazelcast in a Spring Boot application. You can see the code sample here.

What You’ll Learn

In this guide, you’ll see how to quickly set up a Spring Boot application using Hazelcast as Hibernate’s second-level cache.

Prerequisites

  • ~10 minutes

  • A text editor or IDE

  • JDK 1.8+

  • Apache Maven 3.2+

In order to run the code sample, make sure to have PostgresSQL database accessible and configured properly in the application.properties file.

You can spin-up a PostgreSQL instance easily using Docker:

docker run --name 2lc-postgres --publish 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:13

Configuration

In order to enable JPA, you need to add a dedicated Spring Boot Starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

In order to configure Hazelcast as second-level cache provider, you need to add two dependencies:

<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast-hibernate53</artifactId>
    <version>2.1.0</version>
</dependency>

<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast</artifactId>
    <version>4.0.2</version>
</dependency>

And then, we need to configure Hazelcast IMDG local member setting by adding a standard hazelcast.xml file into src/main/resources.

Keep in mind that this might trigger the autoconfiguration of another Hazelcast member so you might want to disable Hazelcast autoconfiguration:

@SpringBootApplication(exclude = HazelcastAutoConfiguration.class)

The last step involves turning on second-level cache by adding two properties into application.properties file:

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory

And now, once you annotate your entity as @Cacheable, it will be cached in Hazelcast member:

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Book { ... }

Running the Application

Run the application by executing the following command.

mvn spring-boot:run

You should see in logs that Hazelcast members started successfully.

Members {size:1, ver:1} [
        Member [172.21.28.181]:5701 - f3984396-0ec9-40c8-861e-cb34f14d7204 this
]

What happens then is that an object is saved to the database and then fetched. In the meantime the object is stored in the second level cache. SecondLevelCacheVisualizer prints out the L2C content in every 10 seconds.

20:56:46.937644
size: 1
    com.hazelcast.hibernate.springhibernate2lc.persistence.Book#1:read-write Item(CacheEntry(com.hazelcast.hibernate.springhibernate2lc.persistence.Book))

20:56:56.997365
size: 1
    com.hazelcast.hibernate.springhibernate2lc.persistence.Book#1:read-write Item(CacheEntry(com.hazelcast.hibernate.springhibernate2lc.persistence.Book))

The object is then evicted from the L2C after 30 seconds, which is the configured time-to-live in hazelcast.xml.

20:57:17.028600
size: 0

Summary

In this guide, we bootstrapped a Spring Boot application which uses Hazelcast as Hibernate’s second-level cache.