2015-01-21 82 views
6
缓存

我使用WildFly 8.1这样JPA 2.1和Hibernate 4.3.5JPA共享缓存/秒的水平WildFly

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

我按照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 并设置共享高速缓存模式,以ENABLE_SELECTIVE

和实体(@Entity)我想缓存都标注有@Cacheable(真),这样的:

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

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

如此看来,共享高速缓存/二级缓存不工作

我错过了什么?


谢谢hwellmann

我试图把hibernate.cache.use_query_cache为True persitence.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="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以便休眠4.3.7,但问题仍然存在

+0

你解决了你的问题吗? – 2016-03-13 22:22:55

+0

您是否启用了统计信息并检查了WildFly管理控制台> Runtime> JPA选项卡? – 2017-05-05 07:43:10

回答

2

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

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

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

,你需要设置查询提示org.hibernate.cacheable=true每个查询被缓存。

+0

谢谢,但不幸的是它并没有为我工作,我编辑我的帖子来解释 – kwisatz 2015-01-22 10:05:43

+0

在查询中设置缓存很重要,也许你可以显示客户端代码,看看你是否做对了吗?或者,为'org.hibernate'启用TRACE日志记录,甚至可以使用'org.infinispan'来查看究竟发生了什么。通过远程调试器来代码也会有所帮助。 – 2015-01-30 14:16:28