title: Why JPA entity refresh is required despite disabling shared cache?
body: <p>I'm using Eclipselink for JPA, and has a webapp which reads the tables in a configured interval. The database is modified externally by a tool to publish some data. Now my webapp makes a getAll() on an Entity and gets the result as a list. However, each time I get the same result list as it was from the first query. Surprisingly, new entries and deleted entries are reflected as expected in the result list. If I update an entry it still gets the original/old result. </p>
  
  <p>From my search, most of the answers in stackoverflow points to JPA level cache, and can be summarized to these</p>
  
  <ul>
  <li>Disable shared cache by settings <code>&lt;shared-cache-mode&gt;NONE&lt;/shared-cache-mode&gt;</code></li>
  <li>Optionally, selectively disable cache on entities by using <code>@Cacheable(false)</code></li>
  <li>Evict cache by doing <code>em.getEntityManagerFactory().getCache().evictAll()</code></li>
  <li>There are some other/old/pre-JPA 2.0 and vendor specific solutions which sets some properties in persistence.xml</li>
  </ul>
  
  <p>However, none of these solutions work in my case. The closest answers I came across was <a href="https://stackoverflow.com/a/15239112/3161271">this, which says that </a></p>
  
  <blockquote>
    <p>Any IDs that are already present in the Session cache are matched up to known entities, while any IDs that are not are populated based on database state. The previously known entities are not refreshed from the database at all.</p>
  </blockquote>
  
  <p>Although this is in the context of Hibernate, I hit the same issue in Eclipselink. So after introducing a refresh on the list I get the expected result.</p>
  
  <pre><code>T objects = getAll(); 
  for (T objects : object) 
      em.refresh(object) 
  
  List&lt;T&gt; getAll() {
    EntityManager em = entityManagerFactory.createEntityManager();
    return  em.createQuery(em.getCriteriaBuilder().createQuery(type)).getResultList();
  }
  </code></pre>
  
  <p>Can someone explain this <code>Session cache</code> in the context of Eclipselink, and explain why <code>shared-cache</code> configuration has no impact on this ? Does a transaction transparently solve this problem (read transaction)? Is there an elegant solution/JPA configuration than doing refresh on object each time ?</p>