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
    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

Friday, March 15, 2013

EJB Lite feature supports JPA in Liberty Beta

The WebSphere V8.5.Next Liberty Profile Alpha gave us a first look at a new EJB Lite (ejblite-3.1) feature for the WebSphere Liberty profile.  If you use EJB session beans to drive your persistence layer you may have been disappointed since the EJB Lite capability in the Alpha didn't include support for JEE-style use of JPA with EJBs.  Attempts at injecting or lookup of JPA entity managers and/or factories within an EJB resulted in an exception.  The use of JPA with Java SE style bootstrapping (Persistence.createEntityManagerFactory()) may have worked if you happened to be using resource local transactions and connection properties (rather than server defined data sources).  This was not an ideal or typical configuration in the application server environment.

But, enough about the Alpha!  I'm happy to report that the recent WebSphere V8.5.Next Liberty Profile Beta includes a major bump in EJB Lite functionality, including integration with JPA.  This integration includes all you might expect, container managed persistence, enlistment in JTA transactions, and support for extended persistence contexts.  The WebSphere Development Tools for Eclipse Beta 9 on top of Eclipse WTP makes it a piece of cake to create new or modify existing EJB and JPA applications.  To enable EJB and JPA in the Liberty Beta just add this feature set to your server.xml (include any other features you may require):
  <featuremanager>
    <feature>ejblite-3.1</feature>
    <feature>jpa-2.0</feature>
  </featuremanager>

Check it out!

-Jeremy