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.