Search code examples
databasecachingeclipselink

Eclipselink cache and Session object


We have disabled cache by setting

eclipselink.cache.shared.default=false

But this change is causing the ReadAllQuery to fail with message

org.eclipse.persistence.exceptions.QueryException
Exception Description: Queries on isolated classes, or queries set to use exclusive connections, must not be executed on a ServerSession or, in CMP, outside of a transaction.

The failing query is

entityManager.unwrap(Session.class).executeQuery(readAllQuery);

I tried setting the following parameters

eclipselink.query-results-cache=false
eclipselink.refresh=true

Still the same exception occurs. Can someone please help with this issue.

1) What is the best way to disable JPA cache and make sure all the queries go to database (to ensure data consistency across all machines). All write/update/read should act on database objects and should be consistent.

2) We are fine with performance overhead in hitting database for all query


Solution

  • The API you are using is giving you the shared serverSession which you should not use for queries since you've disabled the shared cache. Instead, you need the UnitOfWork backing the EntityManager as this will maintain a cache and object for this context.

    entityManager.unwrap(UnitOfWork.class).executeQuery(readAllQuery);