为什么我的 EclipseLink 查询结果缓存不工作

Why is my EclipseLink Query Results Cache NOT working

我正在 TomEE 上开发一个网站,我想使用 EclipseLink 查询结果缓存(L2)工作,但每次我重新加载我的网页时,SELECT 查询是 运行(通过 mysql 的 general_log 检查。

我的数据库实体如下所示:

@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>

我读取数据库 table 如下:

@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,JavaEE6,openJPA 2.4.0,openEJB JavaEE API6.0-6,openEJB 核心 4.7.2,EclipseLink 2.6.2,MySQL 5.6.23, MySQL connector/J 5.1.38 (Tomcat 连接池)

我看过一个类似的问题: Can't get Eclipselink level 2 cache to work 但它没有描述 OP 如何缓存查询结果。

顺便说一句,默认缓存 (L2) em.find(ItemEntity.class, id); 工作正常。

我错过了什么?请帮帮我。

已解决。

我使用的是 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>