6

我正在使用 WildFly 8.1 所以 JPA 2.1 和 Hibernate 4.3.5

我想在 WildFly 中使用 JPA 共享缓存/二级缓存

我遵循 WildFly 文档:https ://docs.jboss.org/author/display/WFLY8/JPA+Reference+Guide#JPAReferenceGuide-UsingtheInfinispansecondlevelcache

这是我的 persitience.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="myAppPU" transaction-type="JTA">
    <jta-data-source>java:/jdbc/myAppDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="org.hibernate.flushMode" value="MANUAL"/>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

我将属性 hibernate.cache.use_second_level_cache 设置为 true 并将 shared-cache-mode 设置为 ENABLE_SELECTIVE

我要缓存的实体(@Entity)用@Cacheable(true)注释,如下所示:

@Entity
@Cacheable(true)
public class Tooltip implements Serializable {
  @Id
  private String path ;
  private String description ;
  private Boolean rendered ;
  //...
}

但是每次我访问一个网页休眠时都会生成很多选择来获取我指示为@Cacheable(true) 的所有实体,即使我已经访问了该页面(在同一个会话中)。

所以似乎共享缓存/二级缓存不起作用

我错过了什么?


谢谢你

我试图在 persitence.xml 中将 hibernate.cache.use_query_cache 设置为 true

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="myAppPU" transaction-type="JTA">
    <jta-data-source>java:/jdbc/myAppDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
      <property name="hibernate.cache.use_query_cache" value="true" />
      <property name="org.hibernate.flushMode" value="MANUAL"/>
    </properties>
  </persistence-unit>
</persistence>

以及我正在使用的查询中的提示

@Entity
@NamedQueries({
    @NamedQuery(name = "FieldInfos.findAll", query = "SELECT i FROM FieldInfos i", hints = {@QueryHint(name="org.hibernate.cacheable",value="true")}),
    @NamedQuery(name = "FieldInfos.findByPath", query = "SELECT i FROM FieldInfos i WHERE i.path = :path", hints = {@QueryHint(name="org.hibernate.cacheable",value="true")})
})
@Cacheable(true)
public class FieldInfos implements Serializable {
    //...
}

但问题仍然存在

我也尝试使用新版本的 WildFly:8.2 所以 Hibernate 4.3.7 但问题仍然存在

4

1 回答 1

2

二级缓存仅影响直接实体查找,对应于EntityManager.find().

如果您试图避免影响缓存实体的各种SELECT查询,您还需要启用查询缓存:

    <property name="hibernate.cache.use_query_cache" value="true" />

并且您需要为org.hibernate.cacheable=true要缓存的每个查询设置查询提示。

于 2015-01-21T17:46:47.007 回答