Why is my EclipseLink Query Results Cache NOT work

2019-08-04 07:58发布

I am developing a website on TomEE, and I want to use the EclipseLink Query Results Cache (L2) working, but every time I reload my web page, SELECT query is running (checked via general_log of mysql).

My Database entity looks like below:

@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) {.......}
}

My persistence.xml looks like below:

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

I read database table like below:

@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();
    }
}

Dependency version details:

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 connector/J 5.1.38 (Tomcat connection pool)

I've looked at a similar question: Can't get Eclipselink level 2 cache to work but it doesn't describe how the OP managed to cache query results.

BTW, the default cache (L2), with em.find(ItemEntity.class, id); is working just fine.

What am I missing? Please help me out.

1条回答
The star\"
2楼-- · 2019-08-04 08:11

Solved.

I was using EclipseLink 2.6.2, I downgraded to version 2.4.2, and now Query Results Cache WORKS as expected!

I guess EclipseLink 2.6.x or 2.5.x is not quite compatible with JPA 2.0.x. It's confusing, because when I use 2.5.x or higher, those still seem to be working, BESIDES the feature of Query Results Cache.

My persistence.xml looks like bellow now, so I can get log output:

<?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>
查看更多
登录 后发表回答