我正在使用eclipselink JPA和一个数据库,该数据库也在我的应用程序外部进行更新。因此,有些表我想每隔几秒钟查询一次。即使尝试禁用缓存和查询缓存,我也无法正常工作。例如:
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());即使在我的应用程序处于睡眠状态时容量发生变化,println也会始终为一个和两个打印相同的值。
Even though the capacity changes while my application is sleeping the println always prints the same value for one and two.
我在persistence.xml中添加了以下内容
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.
詹姆斯
推荐答案问题是您正在阅读PersistenceContext / EM,它维护了以下对象的对象事务视图
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.
将查询刷新属性 eclipselink.refresh添加到find调用(JPA 2.0)或在调用之后简单地调用em.refresh。初步发现。
Add the query refresh property "eclipselink.refresh" to the find call (JPA 2.0) or simply call em.refresh after the initial find.
更多推荐
禁用eclipselink缓存和查询缓存
发布评论