codyLine
codyLine

Reputation: 519

How to clear / refresh the cache in EclipseLink

I'm using following settings in persistence.xml (I use Eclipselink 2.6.4) from my web application:

<properties> 
    <property name="eclipselink.jdbc.cache-statements" value="true" />        
    <property name="eclipselink.cache.query-results" value="true" /> 
    <property name="eclipselink.ddl-generation.index-foreign-keys" value="true" />
    <property name="eclipselink.logging.level" value="OFF" />        
    <property name="eclipselink.persistence-context.close-on-commit" value="true" />        
    <property name="eclipselink.persistence-context.flush-mode" value="commit" />        
    <property name="eclipselink.persistence-context.persist-on-commit" value="false" />
</properties>

I'm using the @Cacheable(false) annotation to prevent some entities from being cached.
The @Cache annotation doesn't work any more in version 2.6.4.
My question is, is there a possibility to clear the cache globally? Let say every 3 hours?
Thanks

Upvotes: 1

Views: 5756

Answers (2)

Bob
Bob

Reputation: 5618

You can clear Eclipselink's second level cache by accessing the Cache object in the EntityManagerFactory:

entityManager.getEntityManagerFactory().getCache().evictAll();

Upvotes: 1

Michael
Michael

Reputation: 172

First level cache is enabled by default and you can not disable it. i.e. no settings in your persistence.xml file will disable first level cache.

You can only clear out all the entity manager objects by calling

entityManager.clear()

this will make subsequent queries go to the database (the first time) and then objects are again stored in the cache

In your case you would need to store your entitymanager objects in a register somewhere and have a loop through it every 3 hours to call the clear() function.

You can force each query to go to the database directly by calling

query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);

Upvotes: 2

Related Questions