Saturday, October 18, 2014

EXP 13: disable cache feature of EclipseLink

Problem
Sometimes, I query data from database using EclipseLink. The query as below:
EntityManager em;
em.createQuery("SELECT o FROM Tbexample as o", Tbexample.class).getResultList();
I get nothing but when I access to database, some record exists in table Tbexample.

Cause
By default, EclipseLink enable caching feature. This feature help increase performance but sometimes it make difficult to control real data in database in very small time (nanosecond). So to ensure the query result show real data on table of database, you have to disable caching feature.

Solution
On persistence.xml, add the line:
<shared-cache-mode>NONE</shared-cache-mode>

On query, add the hint
EntityManager em;
em.createQuery("SELECT o FROM Tbexample as o", Tbexample.class).setHint("javax.persistence.cache.storeMode", "REFRESH").getResultList();
That's all for disable caching feature on querying.

No comments:

Post a Comment