Reputation: 957
I am trying to implement hibernate second level cache in spring-boot 1.5.1 . Facing the below error
Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: 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.
Session cofiguration
hibernateProperties.put("hibernate.cache.use_second_level_cache", true);
hibernateProperties.put("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
hibernateProperties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
JPA POJO
@Entity
@Cacheable
@Cache(usage= CacheConcurrencyStrategy.READ_ONLY, region="messageCache")
@Table(name="AWARD")
public class Award {
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache eternal="true" maxElementsInMemory="1000" overflowToDisk="false" />
<cache name="messageCache" maxElementsInMemory="100" eternal="true" overflowToDisk="false" />
</ehcache>
Upvotes: 1
Views: 6298
Reputation: 5731
It is NOT a classpath issue. It is a missing hibernate configuration. So basically, your session configuration is not reaching hibernate.
I can't tell why because I don't know how hibernate was initialized. I can show you a working Spring-boot implementation though. With Ehcache 3.
and a fully configured cache in JHipster: https://github.com/ehcache/ehcache3-samples/blob/master/fullstack/src/main/java/org/ehcache/sample/config/CacheConfiguration.java
Upvotes: 1
Reputation: 67
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
spring.jpa.properties.hibernate.cache.use_query_cache=true
Had same issue, appending your property with this spring.jpa.properties is important if you are using Spring JPA.
Upvotes: 0
Reputation: 21153
I just tested this using Spring Boot 1.5.1 and Hibernate 5.2.8 using the following settings in hibernate.properties
(even specifying the properties when creating the entity manager bean in java config works too)
hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
I setup a simple entity as follows:
@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "simpleCache")
public class Simple {
}
And specified my ehcache.xml
as follows:
<?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>
The pom.xml things:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.hibernate.stackoverflow</groupId>
<artifactId>stackoverflow</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>stackoverflow</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.8.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Spring starts up just fine:
INFO 8674 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.8.Final}
INFO 8674 --- [ main] org.hibernate.cfg.Environment : HHH000205: Loaded properties from resource hibernate.properties: {hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory, hibernate.cache.use_second_level_cache=true, hibernate.bytecode.use_reflection_optimizer=false}
INFO 8674 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
INFO 8674 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
WARN 8674 --- [ main] c.e.i.s.EhcacheAccessStrategyFactoryImpl : HHH020007: read-only cache configured for mutable entity [simpleCache]
Upvotes: 4
Reputation: 804
You need to add ehcache jar to your class path. The one you listed in the comment above is not the ehcache jar.
This is what you need to add:
https://mvnrepository.com/artifact/net.sf.ehcache/ehcache/2.10.3
Upvotes: 0