2015-01-21 27 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