Search code examples
jpaeclipselink

EclipseLink cache refreshOnlyIfNewer not working


I'm trying to use cache with EclipseLink. I've enabled shared-cache-mode = ENABLE_SELECTIVE in my persistence.xml so when I use @Cacheable(true) the entity gets cached. Now I want make it work when the version is outdated the the enity gets refreshed.

I've setup a optimistic locking field

@Version
@Column(name="optLock")
private int versionNum ;

this is working when as when I try to save an entity that has a older version number then the database I'll get an exception.

So when I add the annotation to enable caching and refresh

@Cacheable(true)
@Cache(size = 500, alwaysRefresh = true, refreshOnlyIfNewer = true)

the entity won't be cached. I've enabled the eclipselink.profiler and I can see that no cache is made for this entity and lacking the Counter:CacheHits and Counter:CacheMisses log.

When I remove

@Cache(size = 500, alwaysRefresh = true, refreshOnlyIfNewer = true)

the entity is cached again but any changes done directly in the database(and increment the optLock field) won't come through.

What I forgetting her? or what should I take into account to get this working? The documentation I've been reading seems like this should be working.


Solution

  • @Cache overrides @Cacheable, so because you did not enable caching in @Cache it gets the persistence unit default of not caching.

    Add,

    @Cache(isolation=SHARED, ...)
    

    to your annotation.

    Please also log a bug, EclipseLink should be smart enough to see the @Cacheable and use it as the default because there was no isolation setting in the @Cache, or at least report a warning.