Error while implementing second-level caching in Rest Spring boot application

941 views Asked by At

I am getting the below exception while running my Rest Spring boot application.

Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath

I have the application.properties and ehcache.xml file in the config directory.

application.properties

#Second level caching
hibernate.cache.use_second_level_cache=true   
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <defaultCache eternal="true" maxElementsInMemory="1000" overflowToDisk="false"/>
    <cache name="simpleCache" maxElementsInMemory="100" eternal="true" overflowToDisk="false" />
</ehcache>

Entity Class:-

import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.Column;
import java.util.Date;
import javax.persistence.Cacheable;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Index;
import javax.persistence.Entity;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "simpleCache")

build.gradle

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    // https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache
    compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '4.0.1.Final'
    // https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core
    compile group: 'net.sf.ehcache', name: 'ehcache-core', version: '2.5.0'
    runtime('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

I am not getting an error if i remove the "@Cache line" in Entity and change "@Cacheable" to "@Cacheable(true)". But in this case each time i get the un change entity data from the DB . The query is being fired.

Any help will be highly appreciated!

2

There are 2 answers

0
Anthony Dahanne On

Your cache does not seem to be used by hibernate.

I'm a bit surprised by the old versions of ehcache and hibernate that you're using :

compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '4.0.1.Final'
compile group: 'net.sf.ehcache', name: 'ehcache-core', version: '2.5.0'

Latest Spring boot defines those versions :

<ehcache3.version>3.2.2</ehcache3.version>
<hibernate.version>5.0.12.Final</hibernate.version>

Anyway, what I suggest you to do, is to embrace those spring boot versions, using spring boot and hibernate jsr-107 (jcache) support to glue them to ehcache3 (you'll need to update your ehcache.xml to the latest version)

I suggest you have a look at this simple spring boot app using ehcache3 and when you're feeling more familiar with spring boot and ehcache / jsr-107, jump to a full fledged jhipster / spring boot / hibernate / ehcache app source code.

0
Henri On

Anthony is absolutely right. That said, I think you direct issue is that the property should be spring.jpa.properties.hibernate.cache.region.factory_class and spring.jpa.properties.hibernate.cache.use_query_cache=true.