Configuring Distributed L2 Hibernate Cache

Table of Contents

Introduction

Hibernate is a powerful object-relational mapping API. Cacheonix cache plugin for Hibernate improves Hibernate performance by reducing unnecessary trips to the database and by hiding the complexity of the caching logic from an application. Cacheonix allows organizations to scale their Hibernate applications linearly by evenly distributing cached data in a cluster and by ensuring that updates to cached data are consistently seen by all servers in the cluster. Cacheonix achieves this by building Hibernate cache on top of Cacheonix intelligent cluster and cache coherence protocol the provides consistent data access even while servers fail or join the cluster.

Hibernate supports several level of caching: Session, Level-2 (L2) and Query. The Session cache is a short-term cache that holds data for access within a single transaction. That is why the Session cache is always local. The L2 cache is used for storing frequently-accessed data. The Query cache holds results of queries executed by Hibernate. Cacheonix plugin for Hibernate takes just a few minutes to configure. Configuring Cacheonix Hibernate plugin includes the following steps:

  1. Downloading Cacheonix
  2. Configuring Entity and Query Caches in Cacheonix
  3. Configuring Hibernate to use Cacheonix Cache Provider

Each of the steps is discussed in the following sections.

Downloading Cacheonix Hibernate Plugin

Visit Downloads section of Cacheonix website to download cacheonix.jar that contains Cacheonix Hibernate plugin. Include the downloaded cacheonix.jar in the classpath. Web applications must put cacheonix.jar in the directory WEB-INF/lib. This is the same directory where hibernate.jar is usually found:

my-webapp.war/
             WEB-INF/
                    lib/
                       cacheonix.jar
                       hibernate.jar

Configuring Entity and Query Caches in Cacheonix

Cacheonix is configured by editing an XML file cacheonix-config.xml that should be included in the classpath. The best location for cacheonix-config.xml in a web application is a directory WEB-INF/classes.

For each entity, add a cache configuration named as fully-qualified name of an entity class. To enabled creating caches for entities that are not configured explicitly, add a cache named 'default'. The following example of cacheonix-config.xml configures caches for tree entities, com.entity.User, com.entity.Order and com.entity.Invoice, and provides a template configuration named 'default' that Cacheonix will use to create caches for entities that don't have explicit cache configurations:

<?xml version ="1.0"?>
<cacheonix xmlns="http://www.cacheonix.com/schema/configuration"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.cacheonix.com/schema/configuration http://www.cacheonix.com/schema/cacheonix-config-2.0.xsd">

   <licensePath path="META-INF/cacheonix-license.xml"/>

   <server>

      <listener>
         <tcp port="8879" buffer="128k"/>
      </listener>

      <broadcast>
         <multicast multicastAddress="225.0.1.2" multicastPort="9998" multicastTTL="1"/>
      </broadcast>

      <partitionedCache name="com.entity.User">
         <store>
            <lru maxElements="1000" maxBytes="1mb"/>
            <expiration timeToLive="60s"/>
         </store>
      </partitionedCache>

      <partitionedCache name="com.entity.Order">
         <store>
            <lru maxElements="5000" maxBytes="5mb"/>
            <expiration timeToLive="120s"/>
         </store>
      </partitionedCache>

      <partitionedCache name="com.entity.Invoice">
         <store>
            <lru maxElements="5000" maxBytes="5mb"/>
            <expiration timeToLive="120s"/>
         </store>
      </partitionedCache>

      <partitionedCache name="default">
         <store>
            <lru maxElements="1000" maxBytes="1mb"/>
            <expiration timeToLive="10s"/>
         </store>
      </partitionedCache>
   </server>
</cacheonix>

Configuring Cacheonix is discussed in detail here.

Configuring Hibernate to use Cacheonix Cache Provider

To enable Cacheonix as a cache provider for Hibernate, set or the configuration property hibernate.cache.provider_class. This property is configured in hibernate.properties or hibernate.cfg.xml, depending on whether you selected a property file or an XML file for configuring Hibernate.

hibernate.properties

hibernate.cache.provider_class=cacheonix.plugin.hibernate.v32.CacheonixCacheProvider

hibernate.cfg.xml

<property name="hibernate.cache.provider_class">cacheonix.plugin.hibernate.v32.CacheonixCacheProvider</property>

For Cacheonix cache provider to work, cacheonix.jar should be added to the application classpath.

Setting Lock Timeout

Cacheonix provides a configuration property cacheonix.lock.timeout.secs that defines how long Cacheonix should wait to obtain a lock. If cacheonix.lock.timeout.secs  is not set, Cacheonix uses a default value of 60 seconds.

hibernate.properties

cacheonix.lock.timeout.secs=120

hibernate.cfg.xml

<property name="cacheonix.lock.timeout.secs">120</property>

See Also

Labels

 
(None)