Spring Data:休眠查询缓存不适用于派生查询
Posted
技术标签:
【中文标题】Spring Data:休眠查询缓存不适用于派生查询【英文标题】:Spring Data: Hibernate query cache not working for derived queries 【发布时间】:2020-04-08 19:05:56 【问题描述】:我正在尝试为 Spring Data 中由方法名称派生的查询设置 Hibernate 查询缓存,但查询缓存从未使用过,甚至似乎没有处于活动状态。
我正在使用 Spring Boot 2.2.1
、Kotlin 1.3.50
和带有 JCache 扩展的 Caffeine 作为我的底层 Hibernate 缓存。
我的仓库如下:
@Repository
interface LibraryRepository : JpaRepository<Library, Long>
@QueryHints(QueryHint(name = CACHEABLE, value = "true"))
override fun findAll(sort: Sort): List<Library>
二级缓存处于活动状态并正常工作,并在库实体上正确设置:
@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "cache.library")
class Library ( ... )
摘自我的配置:
spring:
jpa.properties:
javax.persistence.sharedCache.mode: ENABLE_SELECTIVE
hibernate:
generate_statistics: true
session.events.log: true
hibernate:
cache:
use_second_level_cache: true
use_query_cache: true
region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
javax.cache.provider: com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider
但是查询libraryRepository.findAll(Sort.by("name"))
时,总是查询数据库。
日志中没有提到关于查询缓存的内容。
第一次调用(应该放入缓存):
2019-12-16 18:14:09.955 DEBUG 20400 --- [nio-8080-exec-1] org.hibernate.SQL :
select
library0_.id as id1_3_,
library0_.created_date as created_2_3_,
library0_.last_modified_date as last_mod3_3_,
library0_.name as name4_3_,
library0_.root as root5_3_
from
library library0_
order by
library0_.name asc
2019-12-16 18:14:09.956 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Caching data from load [region=`cache.library` (AccessType[read-write])] : key[org.gotson.komga.domain.model.Library#73] -> value[CacheEntry(org.gotson.komga.domain.model.Library)]
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Checking writeability of read-write cache item [timestamp=`6457308078116864`, version=`null`] : txTimestamp=`6457308159795200`, newVersion=`null`
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Cache put-from-load [region=`AccessType[read-write]` (cache.library), key=`org.gotson.komga.domain.model.Library#73`, value=`CacheEntry(org.gotson.komga.domain.model.Library)`] failed due to being non-writable
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Caching data from load [region=`cache.library` (AccessType[read-write])] : key[org.gotson.komga.domain.model.Library#3] -> value[CacheEntry(org.gotson.komga.domain.model.Library)]
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Checking writeability of read-write cache item [timestamp=`6457308078116864`, version=`null`] : txTimestamp=`6457308159795200`, newVersion=`null`
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Cache put-from-load [region=`AccessType[read-write]` (cache.library), key=`org.gotson.komga.domain.model.Library#3`, value=`CacheEntry(org.gotson.komga.domain.model.Library)`] failed due to being non-writable
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Caching data from load [region=`cache.library` (AccessType[read-write])] : key[org.gotson.komga.domain.model.Library#33] -> value[CacheEntry(org.gotson.komga.domain.model.Library)]
2019-12-16 18:14:09.957 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Checking writeability of read-write cache item [timestamp=`6457308078116864`, version=`null`] : txTimestamp=`6457308159795200`, newVersion=`null`
2019-12-16 18:14:09.958 DEBUG 20400 --- [nio-8080-exec-1] o.h.c.s.support.AbstractReadWriteAccess : Cache put-from-load [region=`AccessType[read-write]` (cache.library), key=`org.gotson.komga.domain.model.Library#33`, value=`CacheEntry(org.gotson.komga.domain.model.Library)`] failed due to being non-writable
2019-12-16 18:14:09.958 DEBUG 20400 --- [nio-8080-exec-1] o.h.stat.internal.StatisticsImpl : HHH000117: HQL: select generatedAlias0 from Library as generatedAlias0 order by generatedAlias0.name asc, time: 3ms, rows: 3
2019-12-16 18:14:09.989 DEBUG 20400 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json]
2019-12-16 18:14:10.025 DEBUG 20400 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing [[LibraryDto(id=73, name=All, root=file:/C:/Users/groebroe101209/files/all/), LibraryDto(id=3, name=C (truncated)...]
2019-12-16 18:14:10.123 INFO 20400 --- [nio-8080-exec-1] i.StatisticalLoggingSessionEventListener : Session Metrics
35800 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
279300 nanoseconds spent preparing 1 JDBC statements;
154000 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
1256401 nanoseconds spent performing 3 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
3500 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
第二次调用(应该命中缓存):
2019-12-16 18:14:35.180 DEBUG 20400 --- [nio-8080-exec-3] org.hibernate.SQL :
select
library0_.id as id1_3_,
library0_.created_date as created_2_3_,
library0_.last_modified_date as last_mod3_3_,
library0_.name as name4_3_,
library0_.root as root5_3_
from
library library0_
order by
library0_.name asc
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Caching data from load [region=`cache.library` (AccessType[read-write])] : key[org.gotson.komga.domain.model.Library#73] -> value[CacheEntry(org.gotson.komga.domain.model.Library)]
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Checking writeability of read-write cache item [timestamp=`6457308078116864`, version=`null`] : txTimestamp=`6457308263133184`, newVersion=`null`
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Cache put-from-load [region=`AccessType[read-write]` (cache.library), key=`org.gotson.komga.domain.model.Library#73`, value=`CacheEntry(org.gotson.komga.domain.model.Library)`] failed due to being non-writable
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Caching data from load [region=`cache.library` (AccessType[read-write])] : key[org.gotson.komga.domain.model.Library#3] -> value[CacheEntry(org.gotson.komga.domain.model.Library)]
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Checking writeability of read-write cache item [timestamp=`6457308078116864`, version=`null`] : txTimestamp=`6457308263133184`, newVersion=`null`
2019-12-16 18:14:35.181 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Cache put-from-load [region=`AccessType[read-write]` (cache.library), key=`org.gotson.komga.domain.model.Library#3`, value=`CacheEntry(org.gotson.komga.domain.model.Library)`] failed due to being non-writable
2019-12-16 18:14:35.182 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Caching data from load [region=`cache.library` (AccessType[read-write])] : key[org.gotson.komga.domain.model.Library#33] -> value[CacheEntry(org.gotson.komga.domain.model.Library)]
2019-12-16 18:14:35.182 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Checking writeability of read-write cache item [timestamp=`6457308078116864`, version=`null`] : txTimestamp=`6457308263133184`, newVersion=`null`
2019-12-16 18:14:35.182 DEBUG 20400 --- [nio-8080-exec-3] o.h.c.s.support.AbstractReadWriteAccess : Cache put-from-load [region=`AccessType[read-write]` (cache.library), key=`org.gotson.komga.domain.model.Library#33`, value=`CacheEntry(org.gotson.komga.domain.model.Library)`] failed due to being non-writable
2019-12-16 18:14:35.182 DEBUG 20400 --- [nio-8080-exec-3] o.h.stat.internal.StatisticsImpl : HHH000117: HQL: select generatedAlias0 from Library as generatedAlias0 order by generatedAlias0.name asc, time: 2ms, rows: 3
2019-12-16 18:14:35.183 DEBUG 20400 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json]
2019-12-16 18:14:35.183 DEBUG 20400 --- [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing [[LibraryDto(id=73, name=All, root=file:/C:/Users/groebroe101209/files/all/), LibraryDto(id=3, name=C (truncated)...]
2019-12-16 18:14:35.186 INFO 20400 --- [nio-8080-exec-3] i.StatisticalLoggingSessionEventListener : Session Metrics
73400 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
41301 nanoseconds spent preparing 1 JDBC statements;
180701 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
1124001 nanoseconds spent performing 3 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
4000 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
【问题讨论】:
我无法谈论 Spring 和 Hibernate。我的第一步是在我认为应该命中并从那里工作的库中添加断点。 感谢您的意见。实际上我今天早上解决了,我的 yaml 中有一个缩进问题:( 【参考方案1】:我的yaml
有问题,没有正确缩进 (!)
spring:
jpa.properties:
javax:
persistence.sharedCache.mode: ENABLE_SELECTIVE
cache.provider: com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider
hibernate:
generate_statistics: true
session.events.log: false
cache:
use_second_level_cache: true
use_query_cache: true
region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
【讨论】:
以上是关于Spring Data:休眠查询缓存不适用于派生查询的主要内容,如果未能解决你的问题,请参考以下文章
休眠延迟加载不适用于 Spring Boot => 无法延迟初始化角色集合无法初始化代理 - 无会话
Spring data jpa 不适用于自动配置的 entityManagerFactory