2017-09-16 85 views
0

運行我的Rest Spring引導應用程序時出現以下異常。在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(真)」我沒有得到一個錯誤。但在這種情況下,每次我都從數據庫中獲取未更改的實體數據。查詢正在被解僱。

任何幫助將不勝感激!

回答

0

您的緩存似乎並未被hibernate使用。

我有點用舊版本的Ehcache和休眠驚訝您使用:

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引導和ehcache/jsr-107更加熟悉時,跳轉到a full fledged jhipster/spring boot/hibernate/ehcache app source code

0

安東尼是絕對正確的。這就是說,我認爲你直接的問題是,該物業應該是spring.jpa.properties.hibernate.cache.region.factory_classspring.jpa.properties.hibernate.cache.use_query_cache=true

相關問題