public class PersistentObjectPool extends Object
PersistentObject
s. An application generally uses some sort of
persistent store to save its data; this caching mechanism is meant to be
used with those applications that use a relational database (RDMBS) as
the perssitent store.
A problem domain object as represented by a Java class may have its state persisted in one or more rows in one or more tables in the database. The operations of writing an object's state to the RDBMS or reading it back from the RDBMS may be costly, and therefore it should only be done when absolutely necessary.
If a Java class needs access to a persistent object and that object is
already in memory, perhaps as the result of a restore operation by an
unrelated (and hence inaccessible) class, the class would be forced to load
the object again, creating a second instance of it in memory. This can be
very wasteful if it happens frequently. To alleviate this problem, a cache
such as PersistentObjectPool
maintains references to all
PersistentObject
s that are currently in memory. Whenever an
object is retrieved from the pool, it is timestamped. A
PersistentObject
has a ttl (time to live) that
specifies how long it can remain in memory before it is discarded. If the
object ages past its ttl without being retrieved from the pool, it
is removed from the pool. A subsequent retrieval of the object from the
pool will cause it to be reloaded from the persistent store.
Even though an object has expired from the pool, other classes may still have references to it. Those classes may continue to access (and modify) the object, even though the object is not in memory as far as the pool is concerned. In general this should not be a problem, but it will be possible to alleviate it in Java 1.2 through the use of soft references.
If a PersistentObject
is written to the persistent store while
it is still in the pool, retrievals of that object from the pool will
return the old copy of the object unless the old copy expires. To prevent
this problem, the object should be explicitly expired when it is stored.
See the documentation for PersistentObject
for more
information.
PersistentObject
Modifier and Type | Class and Description |
---|---|
private class |
PersistentObjectPool.GarbageCollector |
Modifier and Type | Field and Description |
---|---|
private Object[] |
argList |
private Object[] |
emptyArgList |
private Class[] |
emptyTypeList |
private Thread |
gc |
private Class[] |
idTypeList |
private static PersistentObjectPool |
instance |
private Hashtable |
pool |
Modifier | Constructor and Description |
---|---|
private |
PersistentObjectPool() |
Modifier and Type | Method and Description |
---|---|
void |
expireObjects(long now)
Remove all objects from the pool that have expired by the specified time.
|
static PersistentObjectPool |
getInstance()
Get a reference to the
PersistentObjectPool singleton. |
PersistentObject |
getObject(int id,
Class type)
Retrieve an object from the pool.
|
protected PersistentObject |
loadObject(int id,
Class type)
Load a persistent object from the persistent store.
|
private Class[] emptyTypeList
private Object[] emptyArgList
private Class[] idTypeList
private static PersistentObjectPool instance
private PersistentObjectPool()
public static PersistentObjectPool getInstance()
PersistentObjectPool
singleton.public PersistentObject getObject(int id, Class type) throws SQLException
id
- The ID of the object.type
- The type of the object.null
if the object could
not be found.SQLException
- if the operation failed.protected PersistentObject loadObject(int id, Class type) throws SQLException
restore()
method of the specified class. An
instance of the class is then created with the specified ID as an
argument, and then the object is restored via a call to the
restore()
method.id
- The ID of the object to load.type
- The type of the object that is being loaded.SQLException
- If the operation failed.public void expireObjects(long now)
now
- The current time.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses