Configuring 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.

About this task

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 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 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 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 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.

Enabling and configuring the OpenJPA data cache

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 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. You can preserve evicted elements longer with the SoftReferenceSize property. By default, soft references are unlimited. If you need to, 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 SoftReferenceSize=0 ...
  3. You 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'")/>
    Note: 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. Refer to the org.apache.openjpa.persistence.DataCache Javadoc for more information.
After configuring your data cache, you can use it after you restart your application. For usage information and examples, consult chapter 10, Caching in the OpenJPA reference.

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

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.

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, you 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. For more usage information and examples, consult chapter 10, Caching in the OpenJPA reference.

Extending a cache

OpenJPA provides classes that may be extended for further functionality.
  • 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, you should 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
Refer to chapter 10, Caching in the OpenJPA reference for all caching extensions.



In this information ...


IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic Task topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Aug 31, 2013 2:56:59 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=pix&product=was-nd-dist&topic=tejb_datcacheconfig.dita
File name: tejb_datcacheconfig.html