spring boot 1.5.1- hibernate二级缓存

时间:2017-02-25 15:12:45

标签: hibernate spring-boot ehcache

我试图在spring-boot 1.5.1中实现hibernate二级缓存。面对以下错误

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.

会话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>

4 个答案:

答案 0 :(得分:3)

我刚用hibernate.properties中的以下设置使用Spring Boot 1.5.1和Hibernate 5.2.8对此进行了测试(甚至在java配置中创建实体管理器bean时也指定了属性)

hibernate.cache.use_second_level_cache=true   
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

我按如下方式设置了一个简单的实体:

@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "simpleCache")
public class Simple {
}

并指定我的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>

pom.xml的东西:

<?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开始很好:

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]

答案 1 :(得分:1)

类路径问题。这是一个缺少的休眠配置。所以基本上,你的会话配置没有达到休眠状态。

我不知道为什么,因为我不知道hibernate是如何初始化的。我可以向您展示一个有效的Spring-boot实现。使用Ehcache 3。

https://github.com/spring-projects/spring-petclinic/blob/master/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java

和JHipster中完全配置的缓存: https://github.com/ehcache/ehcache3-samples/blob/master/fullstack/src/main/java/org/ehcache/sample/config/CacheConfiguration.java

答案 2 :(得分:0)

您需要将ehcache jar添加到类路径中。您在上面评论中列出的那个不是ehcache jar。

这是您需要添加的内容:

https://mvnrepository.com/artifact/net.sf.ehcache/ehcache/2.10.3

答案 3 :(得分:0)

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

有同样的问题,如果您使用的是Spring JPA,则在此属性中附加此 spring.jpa.properties 非常重要。