Hibernate第二级缓存不使用JBoss Infinispan自定义区域配置
我们有一个使用
- JBOSS EAP 7.4.4.GA(Wildfly Core 15.0.8.Final-Redhat-00001)
- Hibernate 5.3.25.final-Redhat-00002
第二级高速缓存的配置如jboss文档在“ persistence.xml”中:
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
...
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.region_prefix" value="sskm-v2-region" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.cache.infinispan.statistics" value="true" />
在“ startalone.xml” Infinispan中配置:
<subsystem xmlns="urn:jboss:domain:infinispan:12.0">
...
<cache-container name="hibernate" default-cache="default-sskm-v2-cache" statistics-enabled="true" modules="org.infinispan.hibernate-cache">
<local-cache name="entity">
<heap-memory size="10000"/>
</local-cache>
<local-cache name="local-query">
<heap-memory size="10000"/>
</local-cache>
<local-cache name="timestamps"/>
<local-cache name="pending-puts">
<expiration max-idle="60000"/>
</local-cache>
<local-cache name="default-sskm-v2-cache">
<heap-memory size="1000"/>
<expiration lifespan="1200000" max-idle="1200000"/>
</local-cache>
<local-cache name="query.cache.LegalContext">
<heap-memory size="500"/>
<expiration interval="500" max-idle="1000"/>
</local-cache>
<local-cache name="be.fgov.kszbcss.sskm.model.legalcontext.LegalContext">
<heap-memory size="1000"/>
</local-cache>
</cache-container>
</subsystem>
已修改过期以易于测试:默认的caches不过期,自定义查询缓存1 s后。
使用包装器方法启用了查询缓存以用于相关查询:
private <T> TypedQuery<T> createTypedQuery(CriteriaQuery<T> criteriaQuery) {
TypedQuery<T> typedQuery = getEntityManager().createQuery(criteriaQuery);
if (cacheQueries) {
typedQuery.setHint(QueryHints.HINT_CACHEABLE, true);
if (StringUtils.isNotBlank(cacheRegion)) {
typedQuery.setHint(QueryHints.HINT_CACHE_REGION, cacheRegion);
}
}
return typedQuery;
}
尽管所有这些配置缓存只是拒绝使用自定义“ query.cache.cache.legalcontext”缓存,并使用“ local-Query”的值(默认查询询问)。示例运行:
2022-07-04 10:50:22,229 INFO [org.jboss.as.clustering.infinispan] (default task-1) {} WFLYCLINF0002: Started sskm-v2-region.query.cache.LegalContext cache from hibernate container
2022-07-04 10:50:22,230 DEBUG [org.hibernate.cache.internal.QueryResultsCacheImpl] (default task-1) {} Checking cached query results in region: query.cache.LegalContext
2022-07-04 10:50:22,231 DEBUG [org.hibernate.cache.internal.QueryResultsCacheImpl] (default task-1) {} Query results were not found in cache
...
2022-07-04 10:51:30,599 DEBUG [org.hibernate.cache.internal.QueryResultsCacheImpl] (default task-1) {} Checking cached query results in region: query.cache.LegalContext
2022-07-04 10:51:30,599 DEBUG [org.hibernate.cache.internal.QueryResultsCacheImpl] (default task-1) {} Returning cached query results
很明显,查询正在使用缓存,但是我们看到一个命中率是预期的。我
- 尝试添加/删除区域前缀,但没有成功
- 明确定义了“ persistence.xml”中的infinispan区域工厂,但是没有
- so so so so so so so so so so so so so s o s o so so so so so s o s o s o so so
对实体本身测试并观察到相同的行为(仅使用默认的“实体”配置) so so so so so &amp;查询已成功地缓存,但我根本无法覆盖特定区域的高速缓存配置。这可能是什么原因?
We have a project using
- JBoss EAP 7.4.4.GA (WildFly Core 15.0.8.Final-redhat-00001)
- Hibernate 5.3.25.Final-redhat-00002
Second-level cache is configured as described in JBoss documentation in "persistence.xml":
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
...
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.region_prefix" value="sskm-v2-region" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.cache.infinispan.statistics" value="true" />
In "standalone.xml" Infinispan is configured:
<subsystem xmlns="urn:jboss:domain:infinispan:12.0">
...
<cache-container name="hibernate" default-cache="default-sskm-v2-cache" statistics-enabled="true" modules="org.infinispan.hibernate-cache">
<local-cache name="entity">
<heap-memory size="10000"/>
</local-cache>
<local-cache name="local-query">
<heap-memory size="10000"/>
</local-cache>
<local-cache name="timestamps"/>
<local-cache name="pending-puts">
<expiration max-idle="60000"/>
</local-cache>
<local-cache name="default-sskm-v2-cache">
<heap-memory size="1000"/>
<expiration lifespan="1200000" max-idle="1200000"/>
</local-cache>
<local-cache name="query.cache.LegalContext">
<heap-memory size="500"/>
<expiration interval="500" max-idle="1000"/>
</local-cache>
<local-cache name="be.fgov.kszbcss.sskm.model.legalcontext.LegalContext">
<heap-memory size="1000"/>
</local-cache>
</cache-container>
</subsystem>
Expiration has been modified for easy testing: default caches don't expire, custom query cache expires after 1s.
Query caching is enabled for relevant queries using a wrapper method:
private <T> TypedQuery<T> createTypedQuery(CriteriaQuery<T> criteriaQuery) {
TypedQuery<T> typedQuery = getEntityManager().createQuery(criteriaQuery);
if (cacheQueries) {
typedQuery.setHint(QueryHints.HINT_CACHEABLE, true);
if (StringUtils.isNotBlank(cacheRegion)) {
typedQuery.setHint(QueryHints.HINT_CACHE_REGION, cacheRegion);
}
}
return typedQuery;
}
Despite all this configuration caching simply refuses to use the custom "query.cache.LegalContext" cache and uses the values of "local-query" (default query cache). Example run:
2022-07-04 10:50:22,229 INFO [org.jboss.as.clustering.infinispan] (default task-1) {} WFLYCLINF0002: Started sskm-v2-region.query.cache.LegalContext cache from hibernate container
2022-07-04 10:50:22,230 DEBUG [org.hibernate.cache.internal.QueryResultsCacheImpl] (default task-1) {} Checking cached query results in region: query.cache.LegalContext
2022-07-04 10:50:22,231 DEBUG [org.hibernate.cache.internal.QueryResultsCacheImpl] (default task-1) {} Query results were not found in cache
...
2022-07-04 10:51:30,599 DEBUG [org.hibernate.cache.internal.QueryResultsCacheImpl] (default task-1) {} Checking cached query results in region: query.cache.LegalContext
2022-07-04 10:51:30,599 DEBUG [org.hibernate.cache.internal.QueryResultsCacheImpl] (default task-1) {} Returning cached query results
So it's clear the query is using caching but we see a cache hit where a miss was expected. I've
- Attempted to add/remove region prefix but no success
- Explicitly defined the Infinispan region factory in "persistence.xml" but no success
- Tested with entities themselves and observed same behavior (only using default "entity" config)
So entities & queries are successfully being cached, but I'm simply not able to override the cache config for specific regions. What could be the cause for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要覆盖特定区域的高速缓存配置,您需要指出区域如何映射到persistence.xml中的缓存配置:
在您的情况下,您可能想要类似的内容:
有关更多详细信息,请参见:
https:///iinfinispan.orgg/docs/docs/docs/stable/titles/hibernate/hibernate/hibernate/hibernate/hibernate/hibernate/hibernate/hibernate/hibernate-. html
To override the cache configuration for a specific region, you need to indicate how the regions map to cache configurations in your persistence.xml:
In your case, you probably want something like:
For further details, see:
https://infinispan.org/docs/stable/titles/hibernate/hibernate.html