db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject cvs commit: db-ojb/xdocs howto-work-with-clustering.xml
Date Sat, 14 Jun 2003 22:06:02 GMT
thma        2003/06/14 15:06:01

  Added:       xdocs    howto-work-with-clustering.xml
  Log:
  add a new howto on clustering and using the Coherence distributed cache.
  Thanks to Jason McKerr for this contribution
  
  Revision  Changes    Path
  1.1                  db-ojb/xdocs/howto-work-with-clustering.xml
  
  Index: howto-work-with-clustering.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <document>
  
    <properties>
      <author email="mckerrj@nacse.org">Jason McKerr</author>
      <title>Using Tangosol Coherence Clustered Cache with OJB</title>
    </properties>
  
  <body>
  
  <section name="How to use OJB in clustered environments">
  Editorial note: This howto is derived from an entry in Jason McKerr's 
  <a href="http://www.freeroller.net/page/jmckerr">BLOG</a>.
  	<subsection name="three steps to cluster OJB">
  		A number of people, myself included, were wondering how to do distributed 
  		caching in Object Relational Bridge (OJB).  
  		For those of you that haven't used it, OJB is an Object Relational Mapping 
  		framework for database persistence; allowing you to seemlessly persist your 
  		plain old java objects to any database.  I've been working with it for sometime, 
  		and it's really robust now.  You can find it at Jakarta OJB. <br/>
  		
  		Tangosol Coherence is a high-performance, and very nice caching product.  
  		I've only just begun using it, but already it seems excellent.  
  		You can download a 30-day evaulation license. <br/>
  		
  		I'm regurgitating some stuff that Thomas Mahler put on the mailing list, 
  		and then adding more that is specific to using Tangosol coherence. 
  		<a href="http://www.mail-archive.com/ojb-user@db.apache.org/msg01228.html">That
mail is here</a>.<br/>
  		
  		Back to the Topic:  There are a few things that you should do in order 
  		to make your OJB application ready for using a cache in a multi-VM or distributed environment.<br/><br/>
  		
  		<b>First:</b> You need to trash the default sequence manager 
  		that you define in your repository.xml file.  
  		That sequence manager grabs sequences in blocks from an OJB defined table 
  		that keeps track of the information.  However, in a clustered/distributed 
  		environment you'll get conflicts using that. 
  		The default is "SequenceManagerHighLowImpl."  
  		With this, you'll then need to add the "sequence-name" to your primary key definition.
 
  		Here's a sample primary key column:
  		
  		<source><![CDATA[
  <field-descriptor 
  	name="ackID" 
  	column="ACKID" 
  	jdbc-type="INTEGER" 
  	primarykey="true" 
  	autoincrement="true"
  	sequence-name="ackSeq"
  />
  		]]></source>
  		
  		This will use a database defined sequence that is on your database.  
  		Currently, according to Armin Waibel who developed a bunch of this stuff, 
  		if the sequence that you've created in the database does not match to the name given here,

  		you will not get an exception.  
  		OJB will create the sequence for you, I'll have to confirm that though.  
  		I can confirm that the application kept working.  
  		I just didn't check to see if it was using a new sequence or if it failed-over 
  		to the SequenceManagerHighLowImpl or something else.<br/><br/>
  		
  		<b>Second:</b> You need to secure the data at the database.  
  		Thomas Mahler (lead OJB developer and considerable ORM guru) 
  		recommended in one email that you use optimistic locking that is provided by OJB.  
  		Sounds good to me.  To add optimistic locking to an OJB application, 
  		just add a TimeStamp or Integer column to your table.  
  		Then add a mapping in your repository file that adds locking="true". Example:
  		<source><![CDATA[		
   <field-descriptor 
  	name="ackOptLock" 
  	column="ACKOPTLOCK" 
  	jdbc-type="INTEGER" 
  	locking="true"
   />]]></source>		
  		OJB will then handle the locking for you.  No explicit transactional code necessary.<br/><br/>
  		
  		<b>Third: </b>Do The Cache: You're basically in good shape at this point.
 
  		OJB comes with JCS (Also from Jakarta as part of the Turbine project).  
  		I really didn't like the JCS implementation, which is why I'm writing this.  
  		You can, at this point, use any caching; role your own, buy one, use JCS.  
  		I'm not going to talk about JCS setup here.<br/>
  	</subsection>
  	
  	
  	<subsection name="Using the Tangosol Coherence cache as distributed cache in OJB">
  		For Coherence: You need to download Coherence and add the coherence jar's to your classpath.
 
  		Then you need to create a class that uses Coherence as your caching mechanism.  
  		Included at the end of this is a class called ObjectCacheCoherenceImpl. 
  		You'd need to edit your OJB.properties file to use this class.  
  		Here's mine (with packages) 
  		<source>ObjectCacheClass=org.nacse.jlib.ObjectCacheCoherenceImpl</source>
  		
  		Here's the sample class.  Just add it, maybe change the package names, and go.  
  		I used Orion and JRun to do testing. 
  		Just deploy your app to two instances of the app-server and start-em up.  
  		You might want to add host and port number configs for Coherence:
  		
  		<source>-Dtangosol.coherence.localport=8089 and -Dtangosol.coherence.localhost=192.168.0.101</source>
  		
  		Source for <code>org.nacse.jlib.ObjectCacheCoherenceImpl</code>.
  		
  <source><![CDATA[
  package org.nacse.jlib;
  
  import com.tangosol.net.CacheFactory;
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.cache.ObjectCache;
  import org.apache.ojb.broker.cache.RuntimeCacheException;
  
  import java.util.Map;
  
  public class ObjectCacheCoherenceImpl implements ObjectCache {
  
    private Map mapCache;
  
    public ObjectCacheCoherenceImpl() {
    }
  
    public ObjectCacheCoherenceImpl(PersistenceBroker broker) {
      mapCache = CacheFactory.getDistributedCache();
    }
  
    public void cache(Identity oid, Object obj) {
      try {
        mapCache.put(oid.toString(), obj);
      }
      catch (Exception e) {
        throw new RuntimeCacheException(e.getMessage());
      }
    }
  
    public Object lookup(Identity oid) {
      return mapCache.get(oid.toString());
    }
  
    public void remove(Identity oid) {
      try {
        mapCache.remove(oid.toString());
      }
      catch (Exception e) {
        throw new RuntimeCacheException(e.getMessage());
      }
    }
  
    public void clear() {
      if (mapCache != null) {
        try {
          mapCache.clear();
        }
        catch (Exception e) {
          throw new RuntimeCacheException(e);
        }
      }
    }
  }
  ]]></source>
  	
  	</subsection>
  
  </section>
  
  </body>
  </document>
  
  

Mime
View raw message