public class ObjectCacheDefaultImpl extends java.lang.Object implements ObjectCacheInternal, PBStateListener
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.
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
timeout
.
useSoftReferences
.
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.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
AUTOSYNC_PROP |
static java.lang.String |
CACHING_KEY_TYPE_PROP |
static java.lang.String |
SOFT_REFERENCES_PROP |
static java.lang.String |
TIMEOUT_PROP |
TYPE_CACHED_READ, TYPE_NEW_MATERIALIZED, TYPE_TEMP, TYPE_UNKNOWN, TYPE_WRITE
Constructor and Description |
---|
ObjectCacheDefaultImpl(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)
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.
|
void |
cache(Identity oid,
java.lang.Object obj)
Makes object persistent to the Objectcache.
|
boolean |
cacheIfNew(Identity oid,
java.lang.Object obj)
For internal use within ObjectCache implementations or to
build two-level caches.
|
void |
clear()
Clear ObjectCache.
|
void |
doInternalCache(Identity oid,
java.lang.Object obj,
int type)
For internal use.
|
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() |
public static final java.lang.String TIMEOUT_PROP
public static final java.lang.String AUTOSYNC_PROP
public static final java.lang.String CACHING_KEY_TYPE_PROP
public static final java.lang.String SOFT_REFERENCES_PROP
public ObjectCacheDefaultImpl(PersistenceBroker broker, java.util.Properties prop)
public void clear()
clear
in interface ObjectCache
public void doInternalCache(Identity oid, java.lang.Object obj, int type)
ObjectCacheInternal
doInternalCache
in interface ObjectCacheInternal
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 java.lang.Object lookup(Identity oid)
lookup
in interface ObjectCache
oid
- Identity of the object to search for.Identity
is found.public void remove(Identity oid)
remove
in interface ObjectCache
oid
- Identity of the object to be removed.public java.lang.String toString()
toString
in class java.lang.Object
public void beforeRollback(PBStateEvent event)
PBStateListener
beforeRollback
in interface PBStateListener
event
- The event objectpublic void beforeCommit(PBStateEvent event)
PBStateListener
beforeCommit
in interface PBStateListener
event
- The event objectpublic void beforeClose(PBStateEvent event)
PBStateListener
PersistenceBroker
instance will be returned to the pool.beforeClose
in interface PBStateListener
event
- The event objectpublic void afterRollback(PBStateEvent event)
PBStateListener
afterRollback
in interface PBStateListener
event
- The event objectpublic void afterCommit(PBStateEvent event)
PBStateListener
afterCommit
in interface PBStateListener
event
- The event objectpublic void afterBegin(PBStateEvent event)
PBStateListener
afterBegin
in interface PBStateListener
event
- The event objectpublic void beforeBegin(PBStateEvent event)
PBStateListener
beforeBegin
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 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