JPA 2 + EclipseLink: caching issue

I have strange behavior with caching and JPA objects (EclipseLink 2.4.1) + GUICE PERSIST

I will not use caching, however I accidentally get an old instance that I have already changed in the MySQL database.

I tried the following:

  • Add @Cacheable (false) to the JPA object.

  • Disable cache properties in persistence.xml:

<class>MyEntity</class> <shared-cache-mode>NONE</shared-cache-mode> <properties> <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.refresh" value="true"/> <property name="eclipselink.query-results-cache" value="false"/> <property name="eclipselink.weaving" value="false"/> </properties> 

Even activating the EclipseLink trace, I see a JPQL query:

ReadObjectQuery Execute the query (name = "readObject" referenceclass = XX sql = "... (just by making a call to" find "entityManager

but, but randomly returns the old value of this class.

Note

Perhaps it happens that to use different instances of EntityManager and each has its own cache? I saw the following related entry: Disable JPA cache EclipseLink 2.4 If so, you can clear the ALL EntityManager ALL cache without using: ???? . Em.getEntityManagerFactory () getCache () evictAll (). Is it possible to clear ALL caches without using evictALL?

+1
source share
1 answer

Allocate everything to the shared cache that you have already disabled, anyway. EntityManager instances, by default, require their own first-level cache to track all managed instances they created. The Manager entity is intended to represent logical transactions and therefore should not exist for a long time. You need to throw away your EntityManager and get them, or just clear them at logical points, and not allow the number of managed entitites in its cache to grow indefinitely. It will also help limit the problem of outdated data, although nothing more than a pessimistic lock can fix it. I recommend using optimistic locking if you no longer have to overwrite obsolete data.

+1
source

All Articles