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 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.
<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 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 ...
<property name="openjpa.DataCache" value="true(CacheSize=5000 SoftReferenceSize=0 EvictionSchedule='15,45 * * 1'")/>
Refresh with active DataCache
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.
<property name="openjpa.QueryCache" value=("CacheSize=1000, ...
<property name="openjpa.QueryCache" value=("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 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.
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 ... | IBM Redbooks, demos, education, and more(Index) |