drame
drame

Reputation: 545

Eclipselink Query Cache invalidation on cascaded persist

I have a Problem with EclipseLink (2.5.0) QueryCaching or more precisely the invalidation of the query cache if an object is inserted into the database. I got this working fine if I cache a query for Entity A and I insert or update Entity A directly.

However let's assume there is an Entity B which is reference with @OneToMany(mappedBy = " a", orphanRemoval = true, cascade = CascadeType.ALL) from A. A holds a simple List bs; with the annotation. If I add a new B object to the collection bs on A and then merge A with entityManager.merge(); the correct insert into B ... SQL is executed. A subsequent execution of the cached (named) query for all B's Where "something" in the same Transaction does not return the just inserted object.

Obviously the QueryCache of the named query is not updated since the B object was not inserted explicitly but via a Cascade from A.

I found http://www.eclipse.org/eclipselink/api/2.5/org/eclipse/persistence/queries/QueryResultsCachePolicy.html#setInvalidationClasses(java.util.Set) but I have no Idea how to use it. As I understand this, I should be able to add A.class to the InvalidationClasses for the named Query. But entityManager.createNamedQuery() does not offer a Method to set a QueryResultsCachePolicy.

Sadly the documentation of Eclipselink on QueryCaches is outdated and not referencing the used Version 2.5.0.

I use the hint in persistence.xml by the way to enable the Query cache for all named queries. I found this feature via http://java-persistence-performance.blogspot.co.at/2013/06/cool-performance-features-of.html and there seems to be no offical Eclipselink documentation for this. Especially not for the "automatic invalidation of the query cache".

Please advice me on this topic.

Thanks in advance.

Upvotes: 1

Views: 666

Answers (1)

Fábio Almeida
Fábio Almeida

Reputation: 296

To use QueryResultsCachePolicy, you need to do this:

((ReadObjectQuery)((JpaQuery) query).getDatabaseQuery()).setQueryResultsCachePolicy(...);

But to use query cache, you need use session:

JpaHelper.getDatabaseSession(getEntityManager().getEntityManagerFactory()).executeQuery(queryName);

Using Queries and the Cache

EDIT: Using eclipselink.query-results-cache, you´ll need to use @Cache to active cache L2 and edit your persistence.xml

Example:

@Entity
@Cache(isolation=CacheIsolationType.SHARED)
public class A{...}

persistence.xml

<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

Upvotes: 2

Related Questions