Close

UNSPECIFIED and NONE modes of SharedCacheMode

[Updated: Mar 10, 2018, Created: Mar 10, 2018]

The UNSPECIFIED mode of second-level cache can be used to apply the JPA provider specific second-level cache defaults. This is equivalent to not using <shared-cache-mode> at all in persistence.xml.

The mode NONE can be used to disable the second level cache behavior. In this case the JPA provider must not cache any entity in the second-level cache.

Example

In the following example, we are going to use UNSPECIFIED mode with Hibernate.

persistence.xml

src/main/resources/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="example-unit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>UNSPECIFIED</shared-cache-mode>
        <properties>
            <property name="javax.persistence.schema-generation.database.action" value="create"/>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
            <property name="hibernate.cache.region.factory_class"
                      value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
        </properties>
    </persistence-unit>

</persistence>

The Entities

@Entity
@Cacheable(false)
public class Employee{
  @Id
  @GeneratedValue
  private long id;
  private String name;
  private String dept;
    .............
}
@Entity
public class Task {
  @Id
  @GeneratedValue
  private long id;
  private String description;
    .............
}

The main class

public class ExampleMain {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEntities();
          loadEntity();
      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEntities() {
      Employee employee = new Employee();
      employee.setName("Dawn");
      employee.setDept("IT");
      Task task = new Task();
      task.setDescription("UI Coding");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(employee);
      em.persist(task);
      em.getTransaction().commit();
      System.out.println("Employee persisted: " + employee);
      System.out.println("Task persisted: " + task);
      em.close();
      printCacheState();
  }

  private static void loadEntity() {
      EntityManager em = entityManagerFactory.createEntityManager();
      Employee employee = em.find(Employee.class, 1L);
      System.out.println("Employee loaded: " + employee);
      Task task = em.find(Task.class, 2L);
      System.out.println("Task loaded: " + task);
      em.close();
      printCacheState();
  }

  private static void printCacheState() {
      Cache cache = entityManagerFactory.getCache();
      boolean contains = cache.contains(Employee.class, 1L);
      System.out.printf("Cache#contains() for Employee: %5s%n", contains);
      contains = cache.contains(Task.class, 2L);
      System.out.printf("Cache#contains() for Task: %8s%n", contains);
  }
}
Employee persisted: Employee{id=1, name='Dawn', dept='IT'}
Task persisted: Task{id=2, description='UI Coding'}
Cache#contains() for Employee: false
Cache#contains() for Task: false
Employee loaded: Employee{id=1, name='Dawn', dept='IT'}
Task loaded: Task{id=2, description='UI Coding'}
Cache#contains() for Employee: false
Cache#contains() for Task: false

That shows that Hibernate does not enable any kind of second-level cache if UNSPECIFIED mode is used.

Example Project

Dependencies and Technologies Used:

  • h2 1.4.196: H2 Database Engine.
  • hibernate-core 5.2.12.Final: The core O/RM functionality as provided by Hibernate.
    Implements javax.persistence:javax.persistence-api version 2.1
  • hibernate-ehcache 5.2.12.Final: Integration for Ehcache into Hibernate as a second-level caching service.
  • JDK 1.8
  • Maven 3.3.9

Second-Level cache UNSPECIFIED Example Select All Download
  • jpa-second-level-cache-unspecified-mode-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF
            • persistence.xml

    See Also