无法使Eclipselink 2级缓存正常工作 [英] Can't get Eclipselink level 2 cache to work

查看:75
本文介绍了无法使Eclipselink 2级缓存正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个最小的Web应用程序,您可以在这里下载(6Kb): http://www.mediafire.com/?6vo1tc141t65r1g

I have a minimal web app that you can download here (6Kb): http://www.mediafire.com/?6vo1tc141t65r1g

相关配置为:

-eclipselink 2.3.2

-eclipselink 2.3.2

-服务器是tomee 1.0(但glassfish 3.1是相同的)

-server is tomee 1.0 (but glassfish 3.1 is the same)

当我点击页面并按F5键反复刷新时:

When I hit the page and press F5 to refresh repeatedly :

http://localhost:8080/testCache/jsf/test.xhtml

我在控制台中看到了几行

I see several lines like that in the console

[EL Fine]:2012-08-29 19:01:30.821--ServerSession(32981564)-连接(27242067)-线程(线程[http-bio-8080-exec-12,5,main])-选择 id,任务类型FROM任务类型

[EL Fine]: 2012-08-29 19:01:30.821--ServerSession(32981564)--Connection(27242067)--Thread(Thread[http-bio-8080-exec-12,5,main])--SELECT id, tasktype FROM tasktype

通过运行嗅探器,我发现SQL请求始终发送到服务器. 我虽然Eclipselink的2级缓存将返回结果(表中的5行)而不查询数据库. 那么怎么了,我该如何激活缓存呢?

And by running a sniffer I see that SQL requests are always sent to the server. I though the level 2 cache of Eclipselink would return the results (5 rows in a table) without querying the db. So what's wrong, how do I activate the cache ?

文件中的某些摘录

persistence.xml:

persistence.xml :

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
  <persistence-unit name="xxxPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>microsoft</jta-data-source>
    <properties>

        <property name="eclipselink.logging.level" value="FINE"/>
      <property name="eclipselink.logging.parameters" value="true"/>

    </properties>
  </persistence-unit>
</persistence>

执行查询的EJB

/**
 * Some useless class required to use JTA transactions
 * 
 * @author Administrator
 * 
 */
@Stateless
public class Facade {
    @PersistenceContext(unitName = "xxxPU")
    private EntityManager em;


    public List findAll(Class entityClass) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return em.createQuery(cq).getResultList();
    }

    public EntityManager getEntityManager() {
        return em;

    }

}

实体bean:

@Entity
@Table(name = "tasktype")

public class TaskType {

    @Id

    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "tasktype")
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

推荐答案

L2共享缓存,通过该ID缓存对象. find()操作以及通过ID进行的查询将获取缓存命中,而其他查询则不会.生成的对象仍将通过缓存解析,因此一旦缓存了对象,就不会确保对关系进行任何其他查询.

The L2 shared cache, caches objects by there Id. The find() operation, and queries by Id will obtain cache hits, other queries will not. The resulting objects will still be resolved with the cache, so you will not encure any additional queries for relationships once the objects are cached.

您可以在查询中启用缓存,或者(在2.4中)可以为非ID字段建立索引,或者在内存中查询.

You can enable caching on a query, or (in 2.4) you can index non-Id fields, or query in-memory.

看, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Query_Cache

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Query_Options

这篇关于无法使Eclipselink 2级缓存正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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