org.apache.ojb.broker.cache
Class ObjectCacheDefaultImpl

java.lang.Object
  extended byorg.apache.ojb.broker.cache.ObjectCacheDefaultImpl
All Implemented Interfaces:
ObjectCache, PBListener, PBStateListener

public class ObjectCacheDefaultImpl
extends java.lang.Object
implements ObjectCache, PBStateListener

This global ObjectCache stores all Objects loaded by the PersistenceBroker from a DB using a static Map. This means each ObjectCache instance associated with all PersistenceBroker instances use the same Map to cache objects. This could lead in "dirty-reads" (similar to read-uncommitted mode in DB) when a concurrent thread look up same object modified by another thread.
When the PersistenceBroker tries to get an Object by its Identity. It first lookups the cache if the object has been already loaded and cached.
The cache uses soft-references which allows objects (softly) referenced by the cache to be reclaimed by the Java Garbage Collector when they are not longer referenced elsewhere.

Implementation configuration properties:

Property Key Property Values
timeout Lifetime of the cached objects in seconds. If expired the cached object was not returned on lookup call (and removed from cache). Default timeout value is 900 seconds. When set to -1 the lifetime of the cached object depends only on GC and do never get timed out.

NOTE: Objects internal cached via SoftReference, so lifetime of cached object is limited by the GC too.

autoSync If set true all cached/looked up objects within a PB-transaction are traced. If the the PB-transaction was aborted all traced objects will be removed from cache. Default is false.

NOTE: This does not prevent "dirty-reads" (more info see above).

It's not a smart solution for keeping cache in sync with DB but should do the job in most cases.
E.g. if you lookup 1000 objects within a transaction and modify one object and then abort the transaction, 1000 objects will be passed to cache, 1000 objects will be traced and all 1000 objects will be removed from cache. If you read these objects without tx or in a former tx and then modify one object in a tx and abort the tx, only one object was traced/removed.


Version:
$Id: ObjectCacheDefaultImpl.java,v 1.20 2004/01/17 16:55:46 thma Exp $
Author:
Thomas Mahler

Nested Class Summary
(package private)  class ObjectCacheDefaultImpl.CacheEntry
           
 
Field Summary
static java.lang.String AUTOSYNC_PROP
           
protected  PersistenceBroker broker
           
protected static java.util.Map objectTable
          static Map held all cached objects
static java.lang.String TIMEOUT_PROP
           
 
Constructor Summary
ObjectCacheDefaultImpl(PersistenceBroker broker, java.util.Properties prop)
           
 
Method Summary
 void afterBegin(PBStateEvent event)
          Called after a PersistenceBroker transaction was started.
 void afterCommit(PBStateEvent event)
          Called after a PersistenceBroker commit was called.
 void afterOpen(PBStateEvent event)
          Called after the PersistenceBroker instance was obtained from pool.
 void afterRollback(PBStateEvent event)
          Called after a PersistenceBroker rollback was called.
 void beforeBegin(PBStateEvent event)
          Called before a PersistenceBroker transaction was started.
 void beforeClose(PBStateEvent event)
          Called before the PersistenceBroker instance was returned to pool.
 void beforeCommit(PBStateEvent event)
          Called before a PersistenceBroker commit was called.
 void beforeRollback(PBStateEvent event)
          Called before a PersistenceBroker rollback was called.
 void cache(Identity oid, java.lang.Object obj)
          Makes object persistent to the Objectcache.
 void clear()
          Clear ObjectCache.
protected  void finalize()
           
 java.lang.Object lookup(Identity oid)
          Lookup object with Identity oid in objectTable.
 void remove(Identity oid)
          Removes an Object from the cache.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TIMEOUT_PROP

public static final java.lang.String TIMEOUT_PROP
See Also:
Constant Field Values

AUTOSYNC_PROP

public static final java.lang.String AUTOSYNC_PROP
See Also:
Constant Field Values

objectTable

protected static java.util.Map objectTable
static Map held all cached objects


broker

protected PersistenceBroker broker
Constructor Detail

ObjectCacheDefaultImpl

public ObjectCacheDefaultImpl(PersistenceBroker broker,
                              java.util.Properties prop)
Method Detail

clear

public void clear()
Clear ObjectCache. I.e. remove all entries for classes and objects.

Specified by:
clear in interface ObjectCache

cache

public void cache(Identity oid,
                  java.lang.Object obj)
Makes object persistent to the Objectcache. I'm using soft-references to allow gc reclaim unused objects even if they are still cached.

Specified by:
cache in interface ObjectCache

lookup

public java.lang.Object lookup(Identity oid)
Lookup object with Identity oid in objectTable. Returns null if no matching id is found

Specified by:
lookup in interface ObjectCache

remove

public void remove(Identity oid)
Removes an Object from the cache.

Specified by:
remove in interface ObjectCache
Parameters:
oid - Identity of the object to be removed.

toString

public java.lang.String toString()

finalize

protected void finalize()

beforeRollback

public void beforeRollback(PBStateEvent event)
Description copied from interface: PBStateListener
Called before a PersistenceBroker rollback was called.

Specified by:
beforeRollback in interface PBStateListener

beforeCommit

public void beforeCommit(PBStateEvent event)
Description copied from interface: PBStateListener
Called before a PersistenceBroker commit was called.

Specified by:
beforeCommit in interface PBStateListener

beforeClose

public void beforeClose(PBStateEvent event)
Description copied from interface: PBStateListener
Called before the PersistenceBroker instance was returned to pool.

Specified by:
beforeClose in interface PBStateListener

afterRollback

public void afterRollback(PBStateEvent event)
Description copied from interface: PBStateListener
Called after a PersistenceBroker rollback was called.

Specified by:
afterRollback in interface PBStateListener

afterCommit

public void afterCommit(PBStateEvent event)
Description copied from interface: PBStateListener
Called after a PersistenceBroker commit was called.

Specified by:
afterCommit in interface PBStateListener

afterBegin

public void afterBegin(PBStateEvent event)
Description copied from interface: PBStateListener
Called after a PersistenceBroker transaction was started.

Specified by:
afterBegin in interface PBStateListener

beforeBegin

public void beforeBegin(PBStateEvent event)
Description copied from interface: PBStateListener
Called before a PersistenceBroker transaction was started.

Specified by:
beforeBegin in interface PBStateListener

afterOpen

public void afterOpen(PBStateEvent event)
Description copied from interface: PBStateListener
Called after the PersistenceBroker instance was obtained from pool.

Specified by:
afterOpen in interface PBStateListener


Authors: Thomas Mahler and others. (C) 2000 - 2003 Apache Software Foundation
All rights reserved. Published under the Apache License.
http://db.apache.org/ojb
Version: 1.0.rc5, 2003-12-14