OpenJPA and JCache


Apache JCS project now has a new “openjpa” module. This one allows to use JCache (whatever implementation it is) as OpenJPA L2 cache.

To use it simply define in your persistence unit properties the property openjpa.DataCacheManager to jcache:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                       http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="demo">
    <jta-data-source>jdbc/demo</jta-data-source>
    <class>org.superbiz.model.User</class>
    <properties>
      <property name="openjpa.DataCacheManager" value="jcache"/>
    </properties>
  </persistence-unit>
</persistence>

Then the question is how to configure JCache?

First you can put whatever property supports your JCache implementation as persistence unit property. Then you can set jcache.uri property in persistence unit properties to define your JCache configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                       http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="demo">
    <jta-data-source>jdbc/demo</jta-data-source>
    <class>org.superbiz.model.User</class>
    <properties>
      <property name="openjpa.DataCacheManager" value="jcache"/>
      <property name="jcache.uri" value="file:///opt/app/foo/conf/jcache-openejb.properties"/>
    </properties>
  </persistence-unit>
</persistence>

Then to configure each cache you can use the following properties:

  • jcache.store-by-value: is the cache “storeByValue”, default false
  • jcache.read-through: should the cache use a CacheLoader when a missed is encountered
  • jcache.writer-through: should the cache use a CacheWriter
  • jcache.expiry-policy-factory: ExpiryPolicy factory (class name). Default is expires after 5mn
  • jcache.management-enabled: is managedment (JMX) enabled
  • jcache.statistics-enabled: does the cache maintain statistics
Advertisements

One thought on “OpenJPA and JCache

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s