Configuring Distributed iBatis Cache

Overview

The iBatis Data Mapper provides a simple and flexible means of moving data between your Java and .NET objects and a relational database. The Data Mapper framework helps to significantly reduce the amount of Java and .NET code that is normally needed to access a relational database. This framework maps classes to SQL statements using a simple XML descriptor.

Caching improves performance by avoiding making unnecessary trips to the database. To support caching, iBATIS provides a pluggable caching system. Cacheonix offers a plugin for iBatis.  Configuring Cacheonix for iBatis includes the following steps:

  1. Downloading Cacheonix
  2. Configuring iBatis cache model
  3. Setting iBatis mapped statement to use cache model
  4. Configuring Cacheonix cache

The following sections discuss configuring Cacheonix plugin for iBatis in detail.

Downloading Cacheonix

Downloads section of Cacheonix website provides cacheonix.jar that contains Cacheonix iBatis plugin.  Put the downloaded cacheonix.jar in the classpath. Web applications must put cacheonix.jar in the directory WEB-INF/lib.

Configuring Cache Model

iBatis cache model is defined by <cacheModel> element that you add to your DataMapper configuration file. The name of Cacheonix implementation for iBatis cache model is cacheonix.plugin.ibatis.v230.IBatisCacheController .

In the example below product-cache defines a name the cache model that will be used in a mapped statement. cacheonix.plugin.ibatis.v230.IBatisCacheController is Cacheonix implementation of iBatis cache (part of cacheonix.jar). Property name cacheonix.cache.name is a name of the cache configuration in Cacheonix configuration file cacheonix-config.xml.

Example: Configuring iBatis Cache Model to Use Cacheonix Cache Controller

<cacheModel id="product-cache" implementation="cacheonix.plugin.ibatis.v230.IBatisCacheonixCacheController" readOnly="true" serialize="false">
	<flushInterval hours="1"/>
	<flushOnExecute statement="insertProduct"/>
	<flushOnExecute statement="updateProduct"/>
	<flushOnExecute statement="deleteProduct"/>
	<property name="cacheonix.cache.name" value="com.example.Product"/>
</cacheModel>

Setting Mapped Statement to Use Cache

Once the cache model is configured, you can specify the cache model to be used by a mapped statement:

<statement id="getProductList" cacheModel="product-cache">select * from PRODUCT where PRD_CAT_ID = #value#</statement>

Configuring Cacheonix Cache

Cacheonix delegates the rest of the cache configuration to Cacheonix configuration file cacheonix-config.xml that should be included in the classpath. Web applications should put cacheonix-config.xml to WEB-INF/classes.

Example: Configuring cacheonix-config.xml for iBatis

<?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.example.Product">
         <store>
            <lru maxElements="1000" maxBytes="10mb"/>
            <expiration timeToLive="1000ms"/>
         </store>
      </partitionedCache>

      <partitionedCache name="com.example.Invoice">
         <store>
            <lru maxElements="2000" maxBytes="10mb"/>
            <expiration timeToLive="1000ms"/>
         </store>
      </partitionedCache>
   </server>
</cacheonix>

See Also:

Labels

 
(None)