Hibernate: The second-level cache is shared across sessions

It survives the transaction, unlike the first-level cache, and needs a provider plus per-entity opt-in.

Code
// application.yml
spring.jpa.properties.hibernate.cache.use_second_level_cache: true
spring.jpa.properties.hibernate.cache.region.factory_class: jcache

@Entity
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
class Country { @Id String iso; String name; }
Output
Reference data such as countries is read once per node, not once per request.
Mutable, frequently-written entities are the wrong candidates.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11