Configuring OpenJPA caching to improve performance
The OpenJPA implementation gives you the option of storing frequently used data in the memory to improve performance. OpenJPA provides concurrent data and concurrent query caches that support applications to save persistent object data and query results in memory to share among threads and for use in future queries.
About this task
The OpenJPA data cache is a cache of persistent object data that operates at the EntityManagerFactory level. This optional-use cache is designed to increase performance while remaining in full compliance with the Java™ Persistence API (JPA) standard. This means that enabling the caching option can increase the performance of your application, with no changes to your code. The OpenJPA data cache is designed to provide significant performance increases over cacheless operations and ensures that behavior is identical in both cache-enabled and cacheless operations.
When enabled, the cache is examined before accessing the data store. The cache stores data when objects are committed and when persistent objects are loaded from the data store. If operating in a single Java virtual machine (JVM) environment, the JVM maintains and shares a data cache across all EntityManager instances obtained from a particular EntityManagerFactory. The OpenJPA data cache cannot do this in a distributed environment because caches in different JVMs, created from different EntityManagerFactory objects are not synchronized.
Using the OpenJPA cache in a multi-JVM environment can be done by configuring the OpenJPA second
level (L2) cache provider plug-in. See the topic, Dynamic cache provider for the JPA 2.0 second
level cache, and the section Using the dynamic cache L2 Cache Provider in a clustered
environment
, for more information. Configuring the DynaCache plug-in allows for the Data and
Query cache content to be replicated and consistent across JVMs. Other alternatives include setting
up an event notification framework or using a third-party distributed cache such as IBM®
WebSphere® eXtreme Scale.
You can enable the OpenJPA data cache for a single or a multiple JVM environment, set its default element size, including soft references, and specify timeout values.
- To enable the cache for a single JVM, set the openjpa.DataCache property to
true, and set the openjpa.RemoteCommitProvider property to
sjvm:
To enable the data cache in a distributed environment, the openjpa.RemoteCommitProvider must be configured specifically for the environment, or a third-party cache management utility can be used.<property name="openjpa.DataCache" value="true"/> <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
- The maximum cache size can be adjusted by setting the CacheSize
property:
By default, the OpenJPA data cache holds 1000 elements. Objects that are pinned into the cache are not counted when determining if the cache size exceeds its maximum size. If the cache overflows, it evicts random elements. You can preserve evicted elements longer with the SoftReferenceSize property. By default, soft references are unlimited. If you must, you can limit the number of soft references or set to 0 to disable soft references completely:<property name="openjpa.DataCache" value="true(CacheSize=5000...
<property name="openjpa.DataCache" value="true(CacheSize=5000 SoftReferenceSize=0 ...
- You can specify that a cache is cleared at certain times. The EvictionSchedule property of the
OpenJPA cache implementation accepts a cron style eviction schedule. The cron format specifies the
minute, hour of day, day of month, day of month, and day of the week beginning with 1 for Sunday;
the * symbol (asterisk), indicates match all. To schedule a cache to evict at 45 minutes past 3 PM
on Sunday every month you would add this
property:
<property name="openjpa.DataCache" value="true(CacheSize=5000 SoftReferenceSize=0 EvictionSchedule='15,45 * * 1'")/>
- You also can specify a cache timeout value for a single class by setting the timeout metadata
extension to the amount of time in milliseconds that the data of the class is valid; for
example:
Refer to the org.apache.openjpa.persistence.DataCache Javadoc for more information.@Entity @DataCache(timeout=10000) public class Employee { ... }
OpenJPA provides a concurrent query cache that supports applications to save persistent object data and query results in memory to share among threads and for use in future queries. The query cache stores the object IDs returned by query executions. When you run a query, OpenJPA assembles a key based on the query properties and the parameters used at execution time, and checks for a cached query result. If one is found, the object IDs in the cached result are looked up, and the resultant persistence-capable objects are returned. Otherwise, the query is executed against the database, and the object IDs loaded by the query are put into the cache.
You can configure the query cache settings in a similar way to the data cache. The interface provided to the query cache is the org.apache.openjpa.persistence.QueryResultCache class. You can access this class through the OpenJPAEntityManagerFactory.
- Modify the CacheSize property of the openjpa.QueryCache:
<property name="openjpa.QueryCache" value="true("CacheSize=1000, ...
- Change the SoftReferenceSize property to enable and control the size of this
map:
The SoftReferenceSize table is disabled by default. Setting the size enables it.<property name="openjpa.QueryCache" value="true(CacheSize=1000, SoftReferenceSize=100")/>
- Pin or unpin queries in the cache through the QueryResultCache with this syntax:
public void pin(Query q); public void unpin(Query q);
- As previously mentioned, if you want to implement a distributed cache that uses an unsupported method for communications, create an implementation of org.apache.openjpa.event.RemoteCommitProvider.
- If you are adding new behavior, extend org.apache.openjpa.datacache.DataCacheImpl.
- To use your own storage mechanism, extend org.apache.openjpa.datacache.AbstractDataCache.
- To add query functionality, you can extend the default org.apache.openjpa.datacache.QueryCacheImpl.
- Implement your own storage mechanism for query results by extending org.apache.openjpa.datacache.AbstractQueryCache
OpenJPA provides a cache that provides caching of SQL strings used by find operations performed on the entity manager and some queries to manage eagerly fetched relationships. When this cache is enabled, SQL queries used by these operations are generated one time per entity manager factory and can be reused. This cache is enabled by default but can also be configured through the openjpa.jdbc.QuerySQLCache configuration property.
- To use an unmanaged
cache:
<property name="openjpa.jdbc.QuerySQLCache" value="all"/>
- To specify a custom cache
class:
<property name="openjpa.jdbc.QuerySQLCache" value="com.mycompany.MyCustomCache"/>
- To use an unmanaged
cache:
<property name="openjpa.jdbc.QuerySQLCache" value="false"/>