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.
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 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.
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.
<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.
<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 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 SoftReferenceSize=0 ...
<property name="openjpa.DataCache" value="true(CacheSize=5000 SoftReferenceSize=0 EvictionSchedule='15,45 * * 1'")/>
@Entity @DataCache(timeout=10000) public class Employee { ... }Refer to the org.apache.openjpa.persistence.DataCache Javadoc for more information.
Refresh with active DataCache
Query Caching functions
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.
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.
<property name="openjpa.QueryCache" value="true("CacheSize=1000, ...
<property name="openjpa.QueryCache" value="true(CacheSize=1000, SoftReferenceSize=100")/>The SoftReferenceSize table is disabled by default. Setting the size enables it.
public void pin(Query q); public void unpin(Query q);
Extending a cache
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 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.
Configuring or disabling the SQL query cache
<property name="openjpa.jdbc.QuerySQLCache" value="all"/>
<property name="openjpa.jdbc.QuerySQLCache" value="com.mycompany.MyCustomCache"/>
<property name="openjpa.jdbc.QuerySQLCache" value="false"/>
In this information ...Related tasks
Related reference
Related information
| IBM Redbooks, demos, education, and more(Index) |