3.11.1. Data Cache
The OpenJPA data cache is an optional cache of persistent object data that operates at the EntityManagerFactory level.
This cache is designed to significantly increase performance while remaining in full compliance with the JPA standard.
This means that turning on the caching option can transparently increase the performance of your application, with no changes to your code.
OpenJPA’s data cache is not related to the EntityManager cache dictated by the JPA specification.
The JPA specification mandates behavior for the EntityManager cache aimed at guaranteeing transaction isolation when operating on persistent objects.
OpenJPA’s data cache is designed to provide significant performance increases over cacheless operation, while guaranteeing that behavior will be identical in both cache-enabled and cacheless operation.
There are five ways to access data via the OpenJPA APIs: standard relation traversal, large result set relation traversal, queries, looking up an object by id, and iteration over an Extent.
OpenJPA’s cache plugin accelerates three of these mechanisms.
It does not provide any caching of large result set relations or Extent iterators.
If you find yourself in need of higher-performance Extent iteration, see Query Replaces Extent.
| Access method | Uses cache |
|---|---|
Standard relation traversal |
Yes |
Large result set relation traversal |
No |
Query |
Yes |
Lookups by object id |
Yes |
Iteration over an |
No |
When enabled, the cache is checked before making a trip to the datastore. Data is stored in the cache when objects are committed and when persistent objects are loaded from the datastore.
OpenJPA’s data cache can operate in both single-JVM and multi-JVM environments. Multi-JVM caching is achieved through the use of the distributed event notification framework described in Remote Event Notification Framework, or through custom integrations with a third-party distributed cache.
The single JVM mode of operation maintains and shares a data cache across all EntityManager instances obtained from a particular EntityManagerFactory.
This is not appropriate for use in a distributed environment, as caches in different JVMs or created from different EntityManagerFactory objects will not be synchronized.