无法使Eclipselink 2级缓存工作

时间:2012-08-29 12:17:30

标签: java caching jpa eclipselink

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

相关配置为:

-eclipselink 2.3.2

-server是1.0(但是glassfish 3.1是相同的)

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

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

我在控制台中看到了几行这样的行

  

[EL Fine]:2012-08-29   19:01:30.821 - 的ServerSession(32981564) - 连接(27242067) - 线程(线程[HTTP-BIO-8080-EXEC-12,5,主]) - 选择   id,tasktype FROM tasktype

通过运行嗅探器,我发现SQL请求总是发送到服务器。 我虽然Eclipselink的二级缓存会返回结果(表中的5行)而不查询数据库。 那有什么不对,我如何激活缓存?

文件中的一些摘录

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;
    }

}

1 个答案:

答案 0 :(得分:5)

L2共享缓存,通过Id缓存对象。 find()操作和Id的查询将获得缓存命中,其他查询则不会。生成的对象仍将使用缓存进行解析,因此在缓存对象后,您不会对关系进行任何其他查询。

您可以在查询上启用缓存,或者(在2.4中)您可以索引非Id字段,或在内存中查询。

请参阅, 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