【问题标题】:Error while implementing second-level caching in Rest Spring boot application在 Rest Spring 启动应用程序中实现二级缓存时出错
【发布时间】:2023-03-03 04:38:01
【问题描述】:

我在运行我的 Rest Spring 启动应用程序时遇到以下异常。

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

我在 config 目录中有 application.properties 和 ehcache.xml 文件。

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>

实体类:-

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')
}

如果我删除实体中的“@Cache 行”并将“@Cacheable”更改为“@Cacheable(true)”,我不会收到错误消息。但在这种情况下,每次我从数据库中获取未更改的实体数据。正在触发查询。

任何帮助将不胜感激!

【问题讨论】:

    标签: java hibernate spring-boot ehcache second-level-cache


    【解决方案1】:

    安东尼是绝对正确的。也就是说,我认为您的直接问题是属性应该是spring.jpa.properties.hibernate.cache.region.factory_classspring.jpa.properties.hibernate.cache.use_query_cache=true

    【讨论】:

      【解决方案2】:

      hibernate 似乎没有使用您的缓存。

      我对您使用的旧版本的 ehcache 和 hibernate 感到有些惊讶:

      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那些版本:

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

      无论如何,我建议你做的是接受那些 spring boot 版本,使用 spring boot 和 hibernate jsr-107 (jcache) 支持将它们粘合到 ehcache3(你需要update your ehcache.xml to the latest version

      我建议你看看this simple spring boot app using ehcache3,当你对spring boot和ehcache / jsr-107感觉更熟悉时,跳转到a full fledged jhipster / spring boot / hibernate / ehcache app source code

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-08
        • 2020-04-23
        • 1970-01-01
        • 2015-03-14
        • 1970-01-01
        • 2015-10-13
        • 2021-05-18
        • 2020-06-27
        相关资源
        最近更新 更多