Wednesday, April 3, 2013

Monitoring OpenJPA's Caches on the WebSphere Liberty Profile

OpenJPA provides several types of caches that can be used to improve the performance of many applications. Several of OpenJPA's caches collect statistics such as number of entries, hit count, and object type. These attributes can be helpful for monitoring the effectiveness of the cache.  Analysis of these statistics can help you to make decisions about what to cache, how many objects to cache, or even whether to enable caching at all.  An effective caching scheme can produce significant performance benefits, while an ineffective scheme may be a drag on performance and almost certainly increase the memory requirements of the application.  A little tuning can normally go a long way.

Initally, accessing OpenJPA's cache statistics required adding custom Java code to your application.  You were required to call OpenJPA APIs to navigate through the configuration, get access to the caches, and then access their statistics.  As of OPENJPA-1739 (OpenJPA 2.1), these caches are now JMX instrumented.  This means that a application such as JConsole can be be used to monitor cache information out-of-band.  This type of monitoring can be accomplished without changing the application code and with only minor changes to the configuration.  For example, by adding set of properties to your persistence.xml, you can enable instrumentation of the data cache, query result cache, and SQL caches:

<property name="openjpa.Instrumentation
<property name="openjpa.DataCache"
<property name="openjpa.QueryCache"
<property name="openjpa.jdbc.QuerySQLCache"

Using JConsole (provided with some 1.6 and later JDKs), you can attach to your Java program and use the MBeans provided by OpenJPA to view statistics for its caches.  You can also invoke methods on the MBeans to get individual statistics about certain queries or cached object types.  In addition to direct monitoring of the MBeans, the OpenJPA project provides a data cache plugin for JConsole.  The data cache plugin provides a nice interface for monitoring the L2 data cache.

As long as the environment supports JMX, applications running Java SE and Java EE environments can be monitored. The OpenJPA documentation explains how to enable the JMX server in a Java SE environment. This blog contains some information for using JConsole with WebSphere Application Server full profile.

JPA applications deployed on the WebSphere Application Server Liberty Profile version 8.5 and the Liberty V.8.5.Next Beta can also be monitored. Configuration is a piece of cake. You simply need to add the configuration above to your persistence.xml and enable the localConnector-1.0 feature in your Liberty Profile server.xml:

<server description="Liberty Server">
    ... other configuration ...

Start the Liberty application server, and begin using your application. After performing a few operations that use JPA, start up JConsole with the OpenJPA Data Cache plugin and connect to the server. You can find the Liberty Server in the Local process list by searching for for ws-launch.jar followed by the name of your server.

JConsole will connect to the local process and the data cache plugin will discover the PUs available on the server. A tab will be created for each PU it discovers. This screen shows the DataCache statistics for the PollPU persistence unit.

You can also select the MBeans tab and browse through cache statistics of all the caches for the various persistence units in the server. The MBeans also allow you to reset cache statistics and query statistics for individual objects or queries on the fly.