2015-12-30 89 views
1

我正在開發一個關於TomEE的網站,並且我想使用EclipseLink查詢結果緩存(L2),但每次我重新加載我的網頁時,SELECT查詢都在運行(選中通過mysql的general_log)。爲什麼我的EclipseLink查詢結果緩存不工作

我的數據庫實體看起來象下面這樣:

@Entity(name="item") 
@NamedQueries({ 
     @NamedQuery(name="ItemEntity.getAllList", 
       query="Select distinct itemEntity from mypackage.entity.ItemEntity itemEntity", 
       hints={ 
         @QueryHint(name="eclipselink.query-results-cache", value="true"), 
         @QueryHint(name="eclipselink.query-results-cache.size", value="1000"), 
         @QueryHint(name="eclipselink.query-results-cache.expiry", value="10000"), //10 secs for test but not working 
         @QueryHint(name="eclipselink.query-results-cache.type", value="FULL") 
       } 
     ) 
}) 
@org.eclipse.persistence.annotations.Cache(
     type= CacheType.FULL, 
     size=10000, 
     expiry=60000, // 1 minute for test 
     coordinationType= CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS 
) 
public class ItemEntity implements Serializable, Comparable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Integer id; 

    @Column(name="name", unique=true, nullable=false) 
    private String name; 

    /* getters and setters for fields */ 

    public CompanyEntity(){} 

    @Override 
    public int compareTo(Object o) {.......} 
} 

的persistence.xml看起來象下面這樣:

<?xml version="1.0" encoding="UTF-8" ?> 
<persistence version="1.0" 
      xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> 
    <persistence-unit name="myprojectname-persistence-unit" transaction-type="JTA"> 
     <jta-data-source>myprojectname-mysql-jdbc-jta-resource</jta-data-source> 
     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
     <properties> 
      <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform"/> 
      <property name="eclipselink.cache.shared.default" value="true"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

我讀數據庫表如下圖所示:

@Stateful 
public class CompanyDao { 

    @PersistenceContext(unitName = "myprojectname-persistence-unit", type = PersistenceContextType.EXTENDED) 
    protected EntityManager em; 

    public List<CompanyEntity> getAllList(){ 
     return this.em.createNamedQuery("ItemEntity.getAllList", ItemEntity.class).getResultList(); 
    } 
} 

依賴版本詳情:

TomEE 1.7.2,Java EE6,openJPA 2.4.0,openEJB Java EE API 6.0-6,openEJB core 4.7.2,EclipseLink 2.6.2,MySQL 5.6.23,MySQL連接器/ J 5.1.38(Tomcat連接pool)

我看了一個類似的問題: Can't get Eclipselink level 2 cache to work 但它沒有描述OP如何緩存查詢結果。

順便說一句,缺省緩存(L2),與em.find(ItemEntity.class, id);工作得很好。

我錯過了什麼?請幫助我。

+1

什麼是查詢之間的EclipseLink日誌顯示運行? – Chris

+0

@克里斯 - 謝謝!我的問題解決了!我遵循你的建議,並打開日誌輸出(這又把我的幾個我們的,我的舊的依賴jar可能已經阻止了日誌輸出,所以我清理),而查詢之間的日誌沒有什麼問題。但是我從日誌中發現了另一個依賴問題並解決了,所以我會自己發佈詳細的答案。 –

+0

依賴關係很棘手 - EclipseLink 2.6.x與JPA 2.0和2.1兼容,所以降級時可能會清理相關的東西。很高興它解決了。 – Chris

回答

1

已解決。

我正在使用EclipseLink 2.6.2,我降級到版本2.4.2,現在查詢結果緩存工作正常!

我猜EclipseLink 2.6.x或2.5.x與JPA 2.0.x並不完全兼容。 這很令人困惑,因爲當我使用2.5.x或更高版本時,這些仍然可以工作,但卻沒有提供查詢結果緩存功能。

我的persistence.xml看起來像波紋管了,所以我可以得到日誌輸出:

<?xml version="1.0" encoding="UTF-8" ?> 
<persistence version="1.0" 
      xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> 
    <persistence-unit name="myprojectname-persistence-unit" transaction-type="JTA"> 
     <jta-data-source>myprojectname-mysql-jdbc-jta-resource</jta-data-source> 
     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
     <properties> 
      <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform"/> 
      <property name="eclipselink.logging.logger" value="JavaLogger"/> 
      <!-- The warning log of "Problem while registering MBean: java.lang.NullPointerException" did not go away even if I set bellow 2 propertes --> 
      <!-- 
      <property name="eclipselink.register.dev.mbean" value="false" /> 
      <property name="eclipselink.register.run.mbean" value="false" /> 
      --> 

      <property name="eclipselink.cache.shared.default" value="true"/> 
      <property name="eclipselink.logging.parameters" value="true"/> 
      <property name="eclipselink.logging.timestamp" value="true"/> 
      <property name="eclipselink.logging.session" value="true"/> 
      <property name="eclipselink.logging.thread" value="true"/> 
      <property name="eclipselink.logging.exceptions" value="true"/> 
      <property name="eclipselink.logging.level" value="FINEST"/> 
     </properties> 
    </persistence-unit> 
</persistence> 
相關問題