+

Search Tips   |   Advanced Search

Set OpenJPA caching to improve performance


The OpenJPA implementation allows users the option of storing frequently used data in the memory to improve performance. OpenJPA provides concurrent data and concurrent query caches that allow applications to save persistent object data and query results in memory to share among threads and for use in future queries.

OpenJPA data cache functionality

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 the application, with no changes to the code. The OpenJPA data cache is designed to provide significant performance increases over cacheless operations and ensures that behavior will be identical in both cache-enabled and cacheless operations.

When enabled, the cache is examined before accessing the datastore. The cache stores data when objects are committed and when persistent objects are loaded from the datastore. If operating in a single 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 will not be synchronized. Using the OpenJPA cache in a multi-JVM environment requires either using the event notification framework or through custom integrations with a third-party distributed cache utilities such as IBM ObjectGrid.

Enable and configuring the OpenJPA data cache

We 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 set up and configure the OpenJPA data cache, do the following:

  1. To enable the cache for a single JVM, set the openjpa.DataCache property to true, and set the openjpa.RemoteCommitProvider property to sjvm:

    <property name="openjpa.DataCache" value="true"/>
    <property name="openjpa.RemoteCommitProvider" value="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.

  2. The maximum cache size can be adjusted by setting the CacheSize property:

    <property name="openjpa.DataCache" value=true(CacheSize=5000...
    
    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. We can preserve evicted elements longer with the SoftReferenceSize property. By default, soft references are unlimited. to, we can limit the number of soft references or set to 0 to disable soft references completely:

    <property name="openjpa.DataCache" value="true(CacheSize=5000 SoftReferenceSize=0 ...
    

  3. We can specify that a cache should be 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'")/>
    

    We 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. Refer to the org.apache.openjpa.persistence.DataCache Java doc for more information.

After configuring the data cache, we can use it after you restart the application.

Refresh with active DataCache Refreshing an entity may lead to different behavior with or without a DataCache when a separate process or part of the same application are updated or even deleted the corresponding record in the database. By default, entities are refreshed from the database even when DataCache is active. Therefore, with the default configuration the refresh behaves identically with or without a DataCache. However, a persistence unit can be configured to refresh entities from DataCache with the property openjpa.RefreshFromDataCache for improved performance. Under this configuration, any out-of-band changes that occur in the database record will not appear in the refreshed state of the entity.

Regardless of the openjpa.RefreshFromDataCache setting, the DataCache is always bypassed for refresh when locks are active, such as for a pessimistic transaction, in a persistence context. An application may activate openjpa.RefreshFromDataCache but can still bypass the DataCache while refreshing an entity by explicitly evicting the entity from DataCache prior to refresh.

Query Caching functions

OpenJPA provides a concurrent query cache that allows 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.

Configuring or disabling the query cache

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. We can access this class through the OpenJPAEntityManagerFactory. The default query cache implementation caches 100 query executions in a least-recently-used cache. This can be changed by setting the cache size in the CacheSize plugin property. Like the data cache, the query cache also has a backing soft reference map that can be changed using the SoftReferenceSize property. To keep queries in the cache at all times, we can pin them to a cache. To change the query cache properties do the following:

  1. Modify the CacheSize property of the openjpa.QueryCache:

    <property name="openjpa.QueryCache" value=("CacheSize=1000, ...
    

  2. Change the SoftReferenceSize property to enable and control the size of this map:

    <property name="openjpa.QueryCache" value=("CacheSize=1000, SoftReferenceSize=100")/>
    
    The SoftReferenceSize table is disabled by default. Setting the size enables it.

  3. Pin or unpin queries in the cache through the QueryResultCache with this syntax:

    public void pin(Query q); public void unpin(Query q);
    

Modifying these properties allows you to make better use of the query cache.

Extending a cache OpenJPA provides classes that may be extended for further functionality.

OpenJPA query SQL cache

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 once per entity manager factory and may be reused. This cache is enabled by default but can also be configured through the openjpa.jdbc.QuerySQLCache configuration property.

Set or disabling the SQL query cache The query SQL cache can be configured or disabled through the openjpa.jdbc.QuerySQLCache property.

By default, this property is set to true. When set to true the cache is enabled and uses the org.apache.openjpa.util.CacheMap class for its cache store. The CacheMap is a managed cache, meaning that it limits the number of cache entries and has a cache eviction scheme to manage memory usage. If the cache is set to all the org.apache.openjpa.lib.util.ConcurrentHashMap class is used as a cache store. The ConcurrentHashMap is not a managed cache so entries will remain in the cache for the lifetime of an entity manager factory. This caching mechanism may provide better performance at the expense of increased memory usage. A custom cache store class can also be specified provided that it implements the java.util.Map interface. To disable the cache, specify the value false. See the following examples on how to configure or disable the SQL query cache:

 

Next steps

Refer to chapter 10, Caching in the OpenJPA reference for all caching extensions.

 

Related tasks


Develop and packaging JPA applications for a Java SE environment
Task overview: Storing and retrieving persistent data with the Java Persistence API (JPA)