问题
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.
回答1:
<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.
回答2:
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!
回答3:
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.
来源:https://stackoverflow.com/questions/13624823/disable-jpa-eclipselink-2-4-cache