views:

247

answers:

1

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  A: 

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.

Gordon Yorke