public class ObjectCacheTwoLevelImpl extends java.lang.Object implements ObjectCacheInternal, PBStateListener
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). |
Modifier and Type | Class and Description |
---|---|
static interface |
ObjectCacheTwoLevelImpl.CopyStrategy |
static class |
ObjectCacheTwoLevelImpl.CopyStrategyImpl |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
APPLICATION_CACHE_PROP |
static java.lang.String |
COPY_STRATEGY_PROP |
static java.lang.String |
FORCE_PROXIES |
TYPE_CACHED_READ, TYPE_NEW_MATERIALIZED, TYPE_TEMP, TYPE_UNKNOWN, TYPE_WRITE
Constructor and Description |
---|
ObjectCacheTwoLevelImpl(PersistenceBroker broker,
java.util.Properties prop) |
Modifier and Type | Method and Description |
---|---|
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.
|
public static final java.lang.String APPLICATION_CACHE_PROP
public static final java.lang.String COPY_STRATEGY_PROP
public static final java.lang.String FORCE_PROXIES
public ObjectCacheTwoLevelImpl(PersistenceBroker broker, java.util.Properties prop)
public PersistenceBrokerImpl getBroker()
PersistenceBroker
instance associated with
this cache instance.public ObjectCacheInternal getApplicationCache()
public void resetSessionCache()
public void doInternalCache(Identity oid, java.lang.Object obj, int type)
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.doInternalCache
in interface ObjectCacheInternal
public java.lang.Object lookup(Identity oid)
ObjectCache
- Return null if no
object was found.lookup
in interface ObjectCache
oid
- Identity of the object to search for.Identity
is found.public void materializeFullObject(java.lang.Object target)
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.
ObjectCacheTwoLevelImpl.CopyStrategy
.target
- The "flat" object for full materializationpublic void remove(Identity oid)
remove
in interface ObjectCache
oid
- Identity of the object to be removed.public void clear()
clear
in interface ObjectCache
public void cache(Identity oid, java.lang.Object obj)
cache
in interface ObjectCache
oid
- Identity of the object to cache.obj
- The object to cache.public boolean cacheIfNew(Identity oid, java.lang.Object obj)
ObjectCacheInternal
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
.
cacheIfNew
in interface ObjectCacheInternal
oid
- Identity of the object to cache.obj
- The object to cache.public void afterCommit(PBStateEvent event)
afterCommit
in interface PBStateListener
event
- The event objectpublic void beforeClose(PBStateEvent event)
beforeClose
in interface PBStateListener
event
- The event objectpublic void beforeRollback(PBStateEvent event)
beforeRollback
in interface PBStateListener
event
- The event objectpublic void afterOpen(PBStateEvent event)
PBStateListener
PersistenceBroker
instance was
obtained from the pool.afterOpen
in interface PBStateListener
event
- The event objectpublic void beforeBegin(PBStateEvent event)
PBStateListener
beforeBegin
in interface PBStateListener
event
- The event objectpublic void afterBegin(PBStateEvent event)
PBStateListener
afterBegin
in interface PBStateListener
event
- The event objectpublic void beforeCommit(PBStateEvent event)
PBStateListener
beforeCommit
in interface PBStateListener
event
- The event objectpublic void afterRollback(PBStateEvent event)
PBStateListener
afterRollback
in interface PBStateListener
event
- The event object(C) 2002 - 2005 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