Learn to use the WebSphere® Application Server dynamic cache service as a Java Persistence API (JPA) second level (L2) cache provider. JPA 2.0 has standardized the L2 cache interface. WebSphere Application Server supports the JPA standard. The dynamic cache service plugs in as a level 2 cache provider to JPA. This topic describes how the L2 cache boosts the performance of your JPA application, the advantages of using DynaCache as L2 cache provider and how to configure and monitor the dynamic cache service for your JPA application in the WebSphere Application Server environment.
JPA utilizes several configurable L2 caches to maximize performance. The JPA data cache is a cache of persistent object data that operates at the EntityManagerFactory level. When enabled, the data cache is checked before making a trip to the datastore. Data is stored in the cache when objects are committed and when persistent objects are loaded from the datastore. In addition to the data cache, JPA defines service provider interfaces for a query cache.
The query cache stores the object IDs that are returned by query executions. When you run a query, JPA assembles a key that is based on the query properties and the parameters that are used at launch time and checks for a cached query result. If one is found, the object IDs in the cached result are looked up, and the resulting persistence-capable objects are returned. Otherwise, the query is launched against the database and the object IDs that are loaded by the query are placed into the cache. The object ID list is not cached until the list that is returned at query launch time is fully traversed.
The JPA standard provides aliases as a mechanism to easily configure JPA plug-ins. The dynamic cache level 2 cache provider for JPA 2.0 is typically configured with the dynacache alias. You can use the dynacache alias for setting the DataCache and QueryCache properties of JPA. If the QueryCache property is set to use dynacache, then the DataCache property will also use the dynacache alias. You can configure the QueryCache and DataCache properties to use different types of cache providers. You can set the QueryCache property to default and set the DataCache to use dynamic cache, or reverse. It is ideal to use the WebSphere Application Server dynamic cache service for the DataCache, QueryCache, and the DataCacheManager. If the DataCache is set to dynacache, then the RemoteCommitProvider does nothing because DRS and HAM are leveraged to do the cache synchronization. You cannot enable the QueryCache property separately from the DataCache. The QueryCache property benefits from the availability of the DataCache property, where the entity data is cached.
<property name="openjpa.DataCache" value="dynacache(CacheSize=1000)"/> <property name="openjpa.QueryCache" value="dynacache"/> <property name="openjpa.DataCacheManager" value="dynacache"/>
Dynamic cache name | Property name | Alias |
---|---|---|
DataCache cache provider | com.ibm.ws.cache.openjpa. DynacacheDataCache | dynacache |
QueryCache cache provider | com.ibm.ws.cache.openjpa. DynacacheQueryCache | dynacache |
RemoteCommitProvider | com.ibm.ws.cache.openjpa. NoOpRemoteCommitProvider | none |
DataCacheManager | com.ibm.ws.cache.openjpa. DynacacheDataCacheManager | dynacache |
<property name="openjpa.DataCache" value="dynacache(CacheName="myJPACache", CacheSize=1000, EnableDiskOffload=true, DiskCacheSizeInGB=4, DiskOffloadLocation=c:\temp, EnableCacheReplication=true)"/>
Dynamic cache custom properties | Default values |
---|---|
CacheName | default |
CacheSize | 2000 |
EnableDiskOffload | false |
EnableCacheReplication | false |
DiskCacheSizeInGB | not applicable |
DiskOffloadLocation | not applicable |
ReplicationType | 1 , 2 or 4. 1 means NOT_SHARED, 2 is PUSH and 4 is PUSH_PULL |
wsadmin>set mbean [$AdminControl queryNames type=DynaCache,*] wsadmin> $AdminControl invoke $mbean getCacheIDsInMemory {default \S} 66 71 10 A5614-67 wsadmin> $AdminControl invoke $mbean getJPACacheStatistics {OpenBooks openbooks.war OpenBooks default} HIT_COUNT=0 TOTAL_HIT_COUNT=0 READ_COUNT=5 TOTAL_READ_COUNT=5 WRITE_COUNT=4 TOTAL_WRITE_COUNT=4 wsadmin> $AdminControl invoke $mbean getJPACacheStatistics {OpenBooks openbooks.war OpenBooks default openbook.domain.Customer} HIT_COUNT=0 TOTAL_HIT_COUNT=0 READ_COUNT=0 TOTAL_READ_COUNT=0 WRITE_COUNT=1 TOTAL_WRITE_COUNT=1You can also use the Extended Cache Monitor to view the contents of the cache ID and key values that are placed in the cache by the JPA runtime.
Customers can define a cache-instance in the customary way in WebSphere Application Server and then reference the name in the CacheName property of the JPA DataCache property value. You can define an object cache instance using the cacheinstances.properties file, DistributedMapFactory, or the administrative console.
All properties are optional and if they are not specified, defaults properties are assumed. If the cache instance does not exist, the dynamic cache service creates the cache instance. Advanced configuration for a QueryCache Dynacache instance is completed in a similar manner.
The JPA data cache operates in both single-JVM and multi-JVM environments. Multi-JVM caching is achieved through the use of the Data Replication Service (DRS) in a clustered WebSphere Application Server, Network Deployment environment.
For entities and queries in the dynamic cache DataCache and QueryCache instances that are replicated across servers in a WebSphere Application Server, Network Deployment environment using the Data Replication Service, configure a replication domain and associate the replication domain with the cache instance. The persistent unit must also have the openjpa.RemoteCommitProvider openJPA property set to none.
# Significant dynamic cache OpenJPA messages in the SystemOut.log file DYNA1081I: OpenJPA L2 DataCache Dynacache instance \"{0}\" created or retrieved successfully for persistent unit \"{1}\". # Applicable only if QueryCache is enabled DYNA1080I: OpenJPA L2 QueryCache Dynacache instance \"{0}\" created or retrieved successfully for persistent unit \"{1}\".