4

I'm trying to disable EclipseLink 2.4 cache so if the data gets changed in DB by other applications, same data gets refreshed in my application, which is using EclipseLink 2.4, without restarting it. None of these properties seems to work:

<shared-cache-mode>NONE</shared-cache-mode>
...
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.cache.size.default" value="0"/>
<property name="eclipselink.cache.type.default" value="NONE"/>
<property name="eclipselink.query-results-cache" value="false"/>
<property name="eclipselink.refresh" value="true"/>

The only option that helped was:

typedQuery.setHint(QueryHints.REFRESH, HintValues.TRUE);

But this is not an option for me, because now that application is written I don't want to search for all queries or em.find() methods and put this hint in.

EDIT1: Similar problem is described here: http://eclipse.1072660.n5.nabble.com/Notifications-about-external-database-changes-td5389.html

EDIT2: To summarize I would like that all queries and find calls would refresh data taken from database.

Minutis
  • 1,193
  • 1
  • 17
  • 47

3 Answers3

6
<shared-cache-mode>NONE</shared-cache-mode>
or,
<property name="eclipselink.cache.shared.default" value="false"/>

Are the correct mechanisms. Or use the @Cache annotation for a specific class.

I assume you issue is that you are using the same EntityManager for both queries. An EntityManager also is required to maintain a cache of all of the instances it manages. Always refreshing these objects would cause any changes your application had made to be discarded. (An EntityManager is a transactional object).

You should create a new EntityManager per request, or per transaction, or call clear() to discard its managed objects.

EclipseLink also supports WEAK EntityManagers, but the correct design would be to not have long lived EntityManagers.

James
  • 17,965
  • 11
  • 91
  • 146
  • I assure you these two configs that you mentioned have no affect. I tried each and then both of them - the data is still returned from cache. But the idea about "not have long lived EntityManagers" is an interesting one. Because now there is one EntityManager created in whole app, it's created with my own singleton method `getInstance()`. Still I would like to see if there is a solution to disable cache. – Minutis Nov 29 '12 at 15:49
  • Now I would like to mention, that these configs did not solve the question, but solved issue that I was having. If anyone will answer how to disable cache I assure you I will re-accept that answer. The thing that helped me - I used one `EntityManager` that i kept alive in whole application. I redid the way I manage `EntityManager` and now I don't need to disable cache, because each `EntityManager` has clean cache after creation. – Minutis Nov 29 '12 at 17:55
1

I think i have the same issue . I was just trying anyway to clear all caches via the persistence.xml cache properties ( shared-cache, eclipselink-cache ...) , but randomly i got old instances of an Entity, apparently without any pattern.....

No way!

Community
  • 1
  • 1
Azimuts
  • 1,212
  • 4
  • 16
  • 35
0

I create new EntityManager for each request,

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

after changing an entity through JPA in one request, then get the entity in the next request, still get OptimisticLocking issue. Tring to figure out the issue.

Dave
  • 487
  • 1
  • 6
  • 19