JPA在WildFly中共享缓存/二级缓存

时间:2015-01-21 11:17:08

标签: hibernate jpa wildfly second-level-cache shared-cache

我使用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 ;
  //...
}

但每次我访问网页时,hibernate都会生成大量的select来获取我指示为@Cacheable(true)的所有实体,即使我已经访问过该页面(在同一个会话中)。

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

我错过了什么吗?


谢谢你hwellmann

我试图在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但问题仍然存在

1 个答案:

答案 0 :(得分:2)

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

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

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

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