org.apache.ojb.broker.cache
Class ObjectCacheTwoLevelImpl

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

public class ObjectCacheTwoLevelImpl
extends java.lang.Object
implements ObjectCacheInternal, PBStateListener

A two-level ObjectCache implementation with a session- and an application cache. The application cache could be specified by the property applicationCache.

The first level is a transactional session cache which cache objects till PersistenceBroker.close() or if a PB-tx is running till PersistenceBroker.abortTransaction() or PersistenceBroker.commitTransaction(). On commit all objects written to database will be pushed to the application cache.

The session cache act as a temporary storage for all read/store operations of persistent objects and only on commit or close of the used PB instance the buffered objects of type ObjectCacheInternal.TYPE_WRITE will be written to the application cache. Except objects of type ObjectCacheInternal.TYPE_NEW_MATERIALIZED these objects will be immediatly pushed to application cache.

The application cache

Property Key Property Values
applicationCache Specifies the ObjectCache implementation used as application cache (second level cache). By default ObjectCacheDefaultImpl was used. It's recommended to use a shared cache implementation (all used PB instances should access the same pool of objects - e.g. by using a static Map in cache implementation).
copyStrategy Specifies the implementation class of the ObjectCacheTwoLevelImpl.CopyStrategy interface, which was used to copy objects on read and write to application cache. If not specified a default implementation based was used (ObjectCacheTwoLevelImpl.CopyStrategyImpl make field-descriptor based copies of the cached objects).
forceProxies If true on materialization of cached objects, all referenced objects will be represented by proxy objects (independent from the proxy settings in reference- or collection-descriptor).
Note: To use this feature all persistence capable objects have to be interface based or the ProxyFactory and IndirectionHandler implementation classes supporting dynamic proxy enhancement for all classes (see OJB.properties file).

Version:
$Id: ObjectCacheTwoLevelImpl.java 365268 2005-12-31 19:02:50Z arminw $

Nested Class Summary
static interface ObjectCacheTwoLevelImpl.CopyStrategy
           
static class ObjectCacheTwoLevelImpl.CopyStrategyImpl
           
 
Field Summary
static java.lang.String APPLICATION_CACHE_PROP
           
static java.lang.String COPY_STRATEGY_PROP
           
static java.lang.String FORCE_PROXIES
           
 
Fields inherited from interface org.apache.ojb.broker.cache.ObjectCacheInternal
TYPE_CACHED_READ, TYPE_NEW_MATERIALIZED, TYPE_TEMP, TYPE_UNKNOWN, TYPE_WRITE
 
Constructor Summary
ObjectCacheTwoLevelImpl(PersistenceBroker broker, java.util.Properties prop)
           
 
Method Summary
 void afterBegin(PBStateEvent event)
          Called after a transaction was started.
 void afterCommit(PBStateEvent event)
          After committing the transaction push the object from session cache ( 1st level cache) to the application cache (2d level cache).
 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)
          Before closing the PersistenceBroker ensure that the session cache is cleared
 void beforeCommit(PBStateEvent event)
          Called before a transaction will be comitted.
 void beforeRollback(PBStateEvent event)
          Before rollbacking clear the session cache (first level cache)
 void cache(Identity oid, java.lang.Object obj)
          Put the specified object to session cache.
 boolean cacheIfNew(Identity oid, java.lang.Object obj)
          For internal use within ObjectCache implementations or to build two-level caches.
 void clear()
          Clear session cache and application cache.
 void doInternalCache(Identity oid, java.lang.Object obj, int type)
          Cache the given object.
 ObjectCacheInternal getApplicationCache()
          Returns the application cache that this 2-level cache uses.
 PersistenceBrokerImpl getBroker()
          Returns the PersistenceBroker instance associated with this cache instance.
 java.lang.Object lookup(Identity oid)
          Lookup corresponding object from session cache or if not found from the underlying real ObjectCache - Return null if no object was found.
 void materializeFullObject(java.lang.Object target)
          This cache implementation cache only "flat" objects (persistent objects without any references), so when lookup(org.apache.ojb.broker.Identity) a cache object it needs full materialization (assign all referenced objects) before the cache returns the object.
 void remove(Identity oid)
          Remove the corresponding object from session AND application cache.
 void resetSessionCache()
          Discard all session cached objects and reset the state of this class for further usage.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

APPLICATION_CACHE_PROP

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

COPY_STRATEGY_PROP

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

FORCE_PROXIES

public static final java.lang.String FORCE_PROXIES
See Also:
Constant Field Values
Constructor Detail

ObjectCacheTwoLevelImpl

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

getBroker

public PersistenceBrokerImpl getBroker()
Returns the PersistenceBroker instance associated with this cache instance.


getApplicationCache

public ObjectCacheInternal getApplicationCache()
Returns the application cache that this 2-level cache uses.

Returns:
The application cache

resetSessionCache

public void resetSessionCache()
Discard all session cached objects and reset the state of this class for further usage.


doInternalCache

public void doInternalCache(Identity oid,
                            java.lang.Object obj,
                            int type)
Cache the given object. Creates a ObjectCacheTwoLevelImpl.CacheEntry and put it to session cache. If the specified object to cache is of type ObjectCacheInternal.TYPE_NEW_MATERIALIZED it will be immediately pushed to the application cache.

Specified by:
doInternalCache in interface ObjectCacheInternal

lookup

public java.lang.Object lookup(Identity oid)
Lookup corresponding object from session cache or if not found from the underlying real ObjectCache - Return null if no object was 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.

materializeFullObject

public void materializeFullObject(java.lang.Object target)
This cache implementation cache only "flat" objects (persistent objects without any references), so when lookup(org.apache.ojb.broker.Identity) a cache object it needs full materialization (assign all referenced objects) before the cache returns the object. The materialization of the referenced objects based on the auto-XXX settings specified in the metadata mapping.
Override this method if needed in conjunction with a user-defined ObjectCacheTwoLevelImpl.CopyStrategy.

Parameters:
target - The "flat" object for full materialization

remove

public void remove(Identity oid)
Remove the corresponding object from session AND application cache.

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

clear

public void clear()
Clear session cache and application cache.

Specified by:
clear in interface ObjectCache

cache

public void cache(Identity oid,
                  java.lang.Object obj)
Put the specified object to session cache.

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,
                          java.lang.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.

afterCommit

public void afterCommit(PBStateEvent event)
After committing the transaction push the object from session cache ( 1st level cache) to the application cache (2d level cache). Finally, clear the session cache.

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

beforeClose

public void beforeClose(PBStateEvent event)
Before closing the PersistenceBroker ensure that the session cache is cleared

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

beforeRollback

public void beforeRollback(PBStateEvent event)
Before rollbacking clear the session cache (first level cache)

Specified by:
beforeRollback 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

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

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

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

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


(C) 2002 - 2006 Apache Software Foundation
All rights reserved. Published under the Apache License 2.0.
http://db.apache.org/ojb
Version: 1.0.4, 2005-12-30