我正在开发一个关于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>
我读了下面的数据库表:
@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,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连接池)
我看了一个类似的问题: Can't get Eclipselink level 2 cache to work 但它并没有描述OP如何设法缓存查询结果。
BTW,默认缓存(L2),em.find(ItemEntity.class, id);
正常工作。
我错过了什么?请帮帮我。
答案 0 :(得分:1)
解决。
我使用的是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>