org.apache.ojb.broker.cache
Class ObjectCacheDefaultImpl

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

public class ObjectCacheDefaultImpl
extends Object
implements ObjectCacheInternal, 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.

NOTE: By default objects cached via SoftReference which allows objects (softly) referenced by the cache to be reclaimed by the Java Garbage Collector when they are not longer referenced elsewhere, so lifetime of cached object is limited by
- the lifetime of the cache object - see property timeout.
- the garabage collector used memory settings - see property useSoftReferences.
- the maximum capacity of the cache - see property maxEntry.

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.
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.

cachingKeyType Determines how the key was build for the cached objects:
0 - Identity object was used as key, this was the default setting.
1 - Idenity + jcdAlias name was used as key. Useful when the same object metadata model (DescriptorRepository instance) are used for different databases (JdbcConnectionDescriptor)
2 - Identity + model (DescriptorRepository) was used as key. Useful when different metadata model (DescriptorRepository instance) are used for the same database. Keep in mind that there was no synchronization between cached objects with same Identity but different metadata model.
3 - all together (1+2)
useSoftReferences If set true this class use SoftReference to cache objects. Default value is true.

Version:
$Id: ObjectCacheDefaultImpl.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
Author:
Thomas Mahler

Nested Class Summary
(package private) static interface ObjectCacheDefaultImpl.CacheEntry
           
(package private) static class ObjectCacheDefaultImpl.CacheEntryHard
           
(package private) static class ObjectCacheDefaultImpl.CacheEntrySoft
           
(package private) static class ObjectCacheDefaultImpl.OrderedTuple
          Implements equals() and hashCode() for an ordered tuple of constant(!) objects
 
Field Summary
static String AUTOSYNC_PROP
           
protected  PersistenceBroker broker
           
static String CACHING_KEY_TYPE_PROP
           
private  int cachingKeyType
          Determines how the key was build for the cached objects:
0 - Identity object was used as key 1 - Idenity + jcdAlias name was used as key 2 - Identity + model (DescriptorRepository) was used as key 3 - all together (1+2)
private static long failCount
           
private static long gcCount
           
private static long hitCount
           
private  List identitiesInWork
           
private  Logger log
           
protected static Map objectTable
          static Map held all cached objects
private static ReferenceQueue queue
           
static String SOFT_REFERENCES_PROP
           
private  long timeout
          Timeout of the cached objects.
static String TIMEOUT_PROP
           
private  boolean useAutoSync
           
private  boolean useSoftReferences
           
 
Fields inherited from interface org.apache.ojb.broker.cache.ObjectCacheInternal
TYPE_CACHED_READ, TYPE_NEW_MATERIALIZED, TYPE_TEMP, TYPE_UNKNOWN, TYPE_WRITE
 
Constructor Summary
ObjectCacheDefaultImpl(PersistenceBroker broker, Properties prop)
           
 
Method Summary
 void afterBegin(PBStateEvent event)
          Called after a transaction was started.
 void afterCommit(PBStateEvent event)
          Called after a transaction was comitted.
 void afterOpen(PBStateEvent event)
          Called after the PersistenceBroker instance was obtained from the pool.
 void afterRollback(PBStateEvent event)
          Called after a transaction was rolled back.
 void beforeBegin(PBStateEvent event)
          Called before a transaction was started.
 void beforeClose(PBStateEvent event)
          Called before the PersistenceBroker instance will be returned to the pool.
 void beforeCommit(PBStateEvent event)
          Called before a transaction will be comitted.
 void beforeRollback(PBStateEvent event)
          Called before a transaction will be rolled back.
private  ObjectCacheDefaultImpl.CacheEntry buildEntry(Object obj, Identity oid)
           
private  Object buildKey(Identity oid)
           
 void cache(Identity oid, Object obj)
          Makes object persistent to the Objectcache.
 boolean cacheIfNew(Identity oid, Object obj)
          For internal use within ObjectCache implementations or to build two-level caches.
 void clear()
          Clear ObjectCache.
 void doInternalCache(Identity oid, Object obj, int type)
          For internal use.
 Object lookup(Identity oid)
          Lookup object with Identity oid in objectTable.
private  void processQueue()
           
 void remove(Identity oid)
          Removes an Object from the cache.
private  void removeTracedIdentity(Identity oid)
           
private  void synchronizeWithTracedObjects()
           
 String toString()
           
private  void traceIdentity(Identity oid)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

private Logger log

TIMEOUT_PROP

public static final String TIMEOUT_PROP
See Also:
Constant Field Values

AUTOSYNC_PROP

public static final String AUTOSYNC_PROP
See Also:
Constant Field Values

CACHING_KEY_TYPE_PROP

public static final String CACHING_KEY_TYPE_PROP
See Also:
Constant Field Values

SOFT_REFERENCES_PROP

public static final String SOFT_REFERENCES_PROP
See Also:
Constant Field Values

objectTable

protected static final Map objectTable
static Map held all cached objects


queue

private static final ReferenceQueue queue

hitCount

private static long hitCount

failCount

private static long failCount

gcCount

private static long gcCount

broker

protected PersistenceBroker broker

identitiesInWork

private List identitiesInWork

timeout

private long timeout
Timeout of the cached objects. Default was 900 seconds.


useAutoSync

private boolean useAutoSync

cachingKeyType

private int cachingKeyType
Determines how the key was build for the cached objects:
0 - Identity object was used as key 1 - Idenity + jcdAlias name was used as key 2 - Identity + model (DescriptorRepository) was used as key 3 - all together (1+2)


useSoftReferences

private boolean useSoftReferences
Constructor Detail

ObjectCacheDefaultImpl

public ObjectCacheDefaultImpl(PersistenceBroker broker,
                              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

doInternalCache

public void doInternalCache(Identity oid,
                            Object obj,
                            int type)
Description copied from interface: ObjectCacheInternal
For internal use. This method have to be used by all OJB classes to cache objects. It allows to decide if an object should be cached or not. Useful for two level caches to reduce object copy costs.

Specified by:
doInternalCache in interface ObjectCacheInternal

cache

public void cache(Identity oid,
                  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
Parameters:
oid - Identity of the object to cache.
obj - The object to cache.

cacheIfNew

public boolean cacheIfNew(Identity oid,
                          Object obj)
Description copied from interface: ObjectCacheInternal
For internal use within ObjectCache implementations or to build two-level caches. Handle with care.

Used to cache new objects (not already cached) by it's Identity. This method was used to cache new materialized objects and should work as a "atomic" method (the check and the put of the object should be atomic) to avoid concurrency problems.

Currently it's not mandatory that all ObjectCache implementations support this method, so in some cases it's allowed to delegate this method call to the standard cache.

Specified by:
cacheIfNew in interface ObjectCacheInternal
Parameters:
oid - Identity of the object to cache.
obj - The object to cache.
Returns:
If object was added true, else false.

lookup

public 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
Parameters:
oid - Identity of the object to search for.
Returns:
The cached object or null if no matching object for specified Identity is found.

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 String toString()
Overrides:
toString in class Object

traceIdentity

private void traceIdentity(Identity oid)

removeTracedIdentity

private void removeTracedIdentity(Identity oid)

synchronizeWithTracedObjects

private void synchronizeWithTracedObjects()

beforeRollback

public void beforeRollback(PBStateEvent event)
Description copied from interface: PBStateListener
Called before a transaction will be rolled back.

Specified by:
beforeRollback in interface PBStateListener
Parameters:
event - The event object

beforeCommit

public void beforeCommit(PBStateEvent event)
Description copied from interface: PBStateListener
Called before a transaction will be comitted.

Specified by:
beforeCommit in interface PBStateListener
Parameters:
event - The event object

beforeClose

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

Specified by:
beforeClose in interface PBStateListener
Parameters:
event - The event object

afterRollback

public void afterRollback(PBStateEvent event)
Description copied from interface: PBStateListener
Called after a transaction was rolled back.

Specified by:
afterRollback in interface PBStateListener
Parameters:
event - The event object

afterCommit

public void afterCommit(PBStateEvent event)
Description copied from interface: PBStateListener
Called after a transaction was comitted.

Specified by:
afterCommit in interface PBStateListener
Parameters:
event - The event object

afterBegin

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

Specified by:
afterBegin in interface PBStateListener
Parameters:
event - The event object

beforeBegin

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

Specified by:
beforeBegin in interface PBStateListener
Parameters:
event - The event object

afterOpen

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

Specified by:
afterOpen in interface PBStateListener
Parameters:
event - The event object

buildEntry

private ObjectCacheDefaultImpl.CacheEntry buildEntry(Object obj,
                                                     Identity oid)

processQueue

private void processQueue()

buildKey

private Object buildKey(Identity oid)


Copyright © 2007-2011 The Kuali Foundation. All Rights Reserved.