当使用 Hibernate 3.5.6-FINAL 作为我们的 JPA 提供程序并使用 infinispan 4.2.0.ALPHA1 作为二级查询缓存提供程序时,我担心,与 Hibernate 文档相反,数据库结果被多次存储在不同的内存中infinispan namedCache 中的位置(对于返回某些相同记录的不同 HQL 查询的结果集),“local-query”。由于我们的许多频繁发出的查询在其结果集中有很大的交集,这可能会很快耗尽内存并使查询缓存无用。
我怀疑我错误地配置了 infinispan 或 hibernate 或两者,因为我似乎无法让 hibernate 2 级实体缓存作为查询缓存的支持运行。我希望看到一个将 infinispan 作为 hibernate-as-JPA 2 级查询缓存的示例,其结果本身由 infinispan 作为 hibernate-as-JPA 2 级实体缓存支持。
细节:
Hibernate 3.5 文档(http://docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-querycache-enable)声称:
查询缓存不缓存缓存中实际实体的状态;它只缓存标识符值和值类型的结果。出于这个原因,[原文如此] 对于那些预期作为查询结果缓存的一部分缓存的实体,查询缓存应始终与二级缓存一起使用
但是,使用 infinispan (根据http://community.jboss.org/wiki/usinginfinispanasjpahibernatesecondlevelcacheprovider )启用休眠级别 2 查询缓存, 就像在我们的 persistence.xml 中一样:
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
<property name="hibernate.cache.infinispan.statistics" value="true"/>
并检查 infinispan CacheManager JMX 属性仅显示 infinispan-configs.xml 中定义的六个 namedCache 之一(来自 GAV org.hibernate/hibernate-infinispan/3.5.6-FINAL,这取决于 GAV org.infinispan/infinispan-core/ 4.2.0.ALPHA1) 被创建,还有一个没有在那里定义:
org.hibernate.cache.UpdateTimestampsCache(created)
timestamps(not created)
entity-repeatable(not created)
entity(not created)
local-query(created)
replicated-query(not created)
replicated-entity(not created)
我怀疑上面引用的 jboss wiki 文章在讨论实体缓存时指的是 namedCache “实体”;但是,我找不到如何创建该缓存。(除此之外:我还担心创建了 infinispan-configs.xml 的本地查询,但没有创建 infinispan-configs.xml 的时间戳;相反,我们收到了一个 UpdateTimestampsCache,它必须在 hibernate 的其他地方定义。)
<property name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE"/>
在我们的 persistence.xml 中,然后注释相关实体 @javax.persistence.Cacheable 确实(根据 infinispan CacheManager JMX 属性)创建了实体缓存(命名为实体的包限定 java 类名),但它们似乎甚至未使用当 JMX 统计数据显示本地查询的命中率很高时(实际上,这种缓存命中查询的性能非常出色)。
我的恐惧是没有根据的吗?即使在多个 HQL 查询的结果集中返回实体信息,infinispan 也只存储一次实体信息?如果不是,那么获取 infinispan-configs.xml 的 namedCache 的正确方法是什么,“实体”用于避免实体数据的重复存储?最后,如何使用 infinispan-configs.xml 的 namedCache,“timestamps”,而不是“org.hibernate.cache.UpdateTimestampsCache”,作为休眠二级时间戳缓存?