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
value="jmx(Instrument='DataCache,QueryCache,QuerySQLCache')"/>
<property name="openjpa.DataCache"
value="true(EnableStatistics=true)"/>
<property name="openjpa.QueryCache"
value="true(EnableStatistics=true)"/>
<property name="openjpa.jdbc.QuerySQLCache"
value="true(EnableStatistics=true)"/>
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">
<featureManager>
<feature>jsp-2.2</feature>
<feature>jpa-2.0</feature>
<feature>localConnector-1.0</feature>
</featureManager>
... other configuration ...
<server/>
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.
-Jeremy