为什么我的EclipseLink查询结果缓存不工作 [英] Why is my EclipseLink Query Results Cache NOT working

查看:145
本文介绍了为什么我的EclipseLink查询结果缓存不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  @Entity(name =item )
@NamedQueries({
@NamedQuery(name =ItemEntity.getAllList,
query =从mypackage.entity.ItemEntity 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秒测试但不工作
@QueryHint(name =eclipselink.query-results-cache.type,value =FULL)
}

})
@org .eclipse.persistence.annotations.Cache(
type = CacheType.FULL,
size = 10000,
expiry = 60000,//测试1分钟
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;

/ *字段的getter和setter * /

public CompanyEntity(){}

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

我的持久性.xml 如下所示:

 <?xml version =1.0encoding =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-unittransaction-type =JTA>
< jta-data-source> myprojectname-mysql-jdbc-jta-resource< / jta-data-source>
< provider> org.eclipse.persistence.jpa.PersistenceProvider< / provider>
<属性>
< property name =eclipselink.target-databasevalue =org.eclipse.persistence.platform.database.MySQLPlatform/>
< property name =eclipselink.cache.shared.defaultvalue =true/>
< / properties>
< / persistence-unit>
< / persistence>

我读取数据库表如下:

  @Stateful 
public class CompanyDao {

@PersistenceContext(unitName =myprojectname-persistence-unit,type = PersistenceContextType.EXTENDED)
保护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内核4.7.2,EclipseLink 2.6.2,MySQL 5.6.23,MySQL连接器/ J 5.1 .38(Tomcat连接池)



我看过一个类似的问题:
无法获得Eclipselink第2级缓存的工作
但是它没有描述OP如何缓存查询结果。



BTW,默认缓存(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或更高版本时,似乎仍然可以工作,BESIDES查询结果缓存的功能。



我的持久性。 xml现在看起来像现在,所以我可以得到日志输出:

 <?xml version =1.0encoding =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-unittransaction-type =JTA>
< jta-data-source> myprojectname-mysql-jdbc-jta-resource< / jta-data-source>
< provider> org.eclipse.persistence.jpa.PersistenceProvider< / provider>
<属性>
< property name =eclipselink.target-databasevalue =org.eclipse.persistence.platform.database.MySQLPlatform/>
< property name =eclipselink.logging.loggervalue =JavaLogger/>
<! - 注册MBean时出现问题:java.lang.NullPointerException的警告日志即使我设置了bellow 2属性也没有消失 - >
<! -
< property name =eclipselink.register.dev.mbeanvalue =false/>
< property name =eclipselink.register.run.mbeanvalue =false/>
- >

< property name =eclipselink.cache.shared.defaultvalue =true/>
< property name =eclipselink.logging.parametersvalue =true/>
< property name =eclipselink.logging.timestampvalue =true/>
< property name =eclipselink.logging.sessionvalue =true/>
< property name =eclipselink.logging.threadvalue =true/>
< property name =eclipselink.logging.exceptionsvalue =true/>
< property name =eclipselink.logging.levelvalue =FINEST/>
< / properties>
< / persistence-unit>
< / persistence>


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.

解决方案

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>

这篇关于为什么我的EclipseLink查询结果缓存不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆