Disable eclipselink caching and query caching - not working?

此生再无相见时 提交于 2019-12-11 01:23:54

问题


I am using eclipselink JPA with a database which is also being updated externally to my application. For that reason there are tables I want to query every few seconds. I can't get this to work even when I try to disable the cache and query cache. For example:

EntityManagerFactory entityManagerFactory =  Persistence.createEntityManagerFactory("default");
EntityManager em = entityManagerFactory.createEntityManager();

MyLocation one = em.createNamedQuery("MyLocation.findMyLoc").getResultList().get(0);

Thread.sleep(10000);

MyLocation two = em.createNamedQuery("MyLocation.findMyLoc").getResultList().get(0);  

System.out.println(one.getCapacity() + " - " + two.getCapacity());

Even though the capacity changes while my application is sleeping the println always prints the same value for one and two.

I have added the following to the persistence.xml

<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.query-results-cache" value="false"/>

I must be missing something but am running out of ideas.

James


回答1:


The issue is you are reading through the PersistenceContext/EM which maintains an Object Transactional view of the data and will never update unless refreshed.

Add the query refresh property "eclipselink.refresh" to the find call (JPA 2.0) or simply call em.refresh after the initial find.




回答2:


@Entity
@Cache(shared=false)
public class Employee {
    ...
}

I hope it will solve your cache problem.........



来源:https://stackoverflow.com/questions/2921424/disable-eclipselink-caching-and-query-caching-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!