org.apache.ojb.broker.cache
Class TwoLevelCache

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

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

A cache that delegates to another cache in case of misses, and maintains that second-level cache with clones. Work in progress! Known problems:

Version:
$Id: TwoLevelCache.java,v 1.2 2003/11/28 14:40:00 arminw Exp $
Author:
oliverm
See Also:
ObjectCachePerBrokerImpl

Nested Class Summary
(package private) static class TwoLevelCache.NoCloneMethod
           
 
Field Summary
protected  Logger logger
           
protected  java.util.Map objectTable
          the hashtable holding all cached objects
 
Constructor Summary
TwoLevelCache(PersistenceBroker broker, java.util.Properties props)
          OJB calls this constructor by reflection.
 
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)
          We clear the cache
 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)
          If this method is being called while retrieving the object from database, then the references and collections of obj are filled.
 void clear()
          Clear ObjectCache.
static void clearSecondLevel()
          clear the second level cache, which is shared by all instances in the same VM.
protected  java.lang.Object clone(java.lang.Object in)
          this method is invoked when an object in inserted or retrieved from the second level.
protected  DescriptorRepository getDescriptorRepository()
           
protected  java.lang.Object getProxy(java.lang.Class referencedClass, java.lang.Class referencedProxy, java.lang.Object[] pkVals)
           
protected  void initCacheCollectionField(CollectionDescriptor colDesc, java.lang.Object cacheObj, java.lang.Object origObject)
          Sets the value of one collection field when an object is inserted into the cache.
protected  void initLookedUpCollections(java.lang.Object obj, java.lang.Object cacheObj)
          Initializes the the non-embedded collection fields after the object has been retrieved from the cache.
 java.lang.Object lookup(Identity oid)
          Does an extra lookup in the secondLevelCache if the first lookup fails.
 void remove(Identity oid)
          Removes an Object from the cache.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

objectTable

protected java.util.Map objectTable
the hashtable holding all cached objects


logger

protected final Logger logger
Constructor Detail

TwoLevelCache

public TwoLevelCache(PersistenceBroker broker,
                     java.util.Properties props)
OJB calls this constructor by reflection.

Parameters:
props - ignored
Method Detail

getDescriptorRepository

protected final DescriptorRepository getDescriptorRepository()
Returns:
the content of the repository.xml or the like.

clear

public void clear()
Clear ObjectCache. I.e. remove all entries for classes and objects. Do not clear the second-level cache

Specified by:
clear in interface ObjectCache

clearSecondLevel

public static void clearSecondLevel()
clear the second level cache, which is shared by all instances in the same VM.


cache

public void cache(Identity oid,
                  java.lang.Object obj)
If this method is being called while retrieving the object from database, then the references and collections of obj are filled. Problem: if this method is called during insert inside a transaction that is later rolledback, then there are objects entered into the second-level cache that should not be there.

Specified by:
cache in interface ObjectCache

getProxy

protected final java.lang.Object getProxy(java.lang.Class referencedClass,
                                          java.lang.Class referencedProxy,
                                          java.lang.Object[] pkVals)

clone

protected java.lang.Object clone(java.lang.Object in)
                          throws java.lang.CloneNotSupportedException
this method is invoked when an object in inserted or retrieved from the second level. Maybe overridden to do model-specific cloning or the like.

Parameters:
in - some object
Returns:
clone of that object.
Throws:
java.lang.CloneNotSupportedException

initLookedUpCollections

protected void initLookedUpCollections(java.lang.Object obj,
                                       java.lang.Object cacheObj)
Initializes the the non-embedded collection fields after the object has been retrieved from the cache.

Parameters:
obj - the freshly retrieved object
cacheObj - its copy in the cache

initCacheCollectionField

protected void initCacheCollectionField(CollectionDescriptor colDesc,
                                        java.lang.Object cacheObj,
                                        java.lang.Object origObject)
Sets the value of one collection field when an object is inserted into the cache.

Parameters:
colDesc -
cacheObj - the copy of the object in the cache
origObject - the copy of the object in the scope of some PersistenceBroker

lookup

public java.lang.Object lookup(Identity oid)
Does an extra lookup in the secondLevelCache if the first lookup fails.

Specified by:
lookup in interface ObjectCache
See Also:
ObjectCachePerBrokerImpl.lookup(org.apache.ojb.broker.Identity)

toString

public java.lang.String toString()

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.

beforeClose

public void beforeClose(PBStateEvent event)
We clear the cache

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

beforeBegin

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

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

beforeCommit

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

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

beforeRollback

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

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


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