站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Hibernate 2.1.8 正式版 API 英文文档

ReadWriteCache (Hibernate API Documentation) - Hibernate 2.1.8 正式版 API 英文文档


net.sf.hibernate.cache
Class ReadWriteCache

java.lang.Object
  extended bynet.sf.hibernate.cache.ReadWriteCache
All Implemented Interfaces:
CacheConcurrencyStrategy

public class ReadWriteCache
extends Object
implements CacheConcurrencyStrategy

Caches data that is sometimes updated while maintaining the semantics of "read committed" isolation level. If the database is set to "repeatable read", this concurrency strategy almost maintains the semantics. Repeatable read isolation is compromised in the case of concurrent writes. This is an "asynchronous" concurrency strategy.

If this strategy is used in a cluster, the underlying cache implementation must support distributed hard locks (which are held only momentarily). This strategy also assumes that the underlying cache implementation does not do asynchronous replication and that state has been fully replicated as soon as the lock is released.

See Also:
for a faster algorithm, CacheConcurrencyStrategy

Nested Class Summary
static class ReadWriteCache.Item
          An item of cached data, timestamped with the time it was cached,.
static class ReadWriteCache.Lock
          A soft lock which supports concurrent locking, timestamped with the time it was released
static interface ReadWriteCache.Lockable
           
 
Nested classes inherited from class net.sf.hibernate.cache.CacheConcurrencyStrategy
CacheConcurrencyStrategy.SoftLock
 
Constructor Summary
ReadWriteCache()
           
 
Method Summary
 void afterInsert(Object key, Object value, Object version)
          Add the new item to the cache, checking that no other transaction has accessed the item.
 void afterUpdate(Object key, Object value, Object version, CacheConcurrencyStrategy.SoftLock clientLock)
          Re-cache the updated state, if and only if there there are no other concurrent soft locks.
 void clear()
          Evict all items from the cache immediately.
 void destroy()
          Clean up all resources.
 void evict(Object key)
          Do nothing.
 Object get(Object key, long txTimestamp)
          Do not return an item whose timestamp is later than the current transaction timestamp.
 void insert(Object key, Object value)
          Do nothing.
 CacheConcurrencyStrategy.SoftLock lock(Object key, Object version)
          Stop any other transactions reading or writing this item to/from the cache.
 boolean put(Object key, Object value, long txTimestamp, Object version, Comparator versionComparator)
          Do not add an item to the cache unless the current transaction timestamp is later than the timestamp at which the item was invalidated.
 void release(Object key, CacheConcurrencyStrategy.SoftLock clientLock)
          Release the soft lock on the item.
 void remove(Object key)
          Evict an item from the cache immediately (without regard for transaction isolation).
 void setCache(Cache cache)
          Set the underlying cache implementation.
 void setMinimalPuts(boolean minimalPuts)
          Enable "minimal puts" mode for this cache
 void update(Object key, Object value)
          Do nothing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReadWriteCache

public ReadWriteCache()
Method Detail

setCache

public void setCache(Cache cache)
Description copied from interface: CacheConcurrencyStrategy
Set the underlying cache implementation.

Specified by:
setCache in interface CacheConcurrencyStrategy
Parameters:
cache -

get

public Object get(Object key,
                  long txTimestamp)
           throws CacheException
Do not return an item whose timestamp is later than the current transaction timestamp. (Otherwise we might compromise repeatable read unnecessarily.) Do not return an item which is soft-locked. Always go straight to the database instead.

Note that since reading an item from that cache does not actually go to the database, it is possible to see a kind of phantom read due to the underlying row being updated after we have read it from the cache. This would not be possible in a lock-based implementation of repeatable read isolation. It is also possible to overwrite changes made and committed by another transaction after the current transaction read the item from the cache. This problem would be caught by the update-time version-checking, if the data is versioned or timestamped.

Specified by:
get in interface CacheConcurrencyStrategy
Parameters:
key -
txTimestamp - a timestamp prior to the transaction start time
Returns:
the cached object or null
Throws:
CacheException

lock

public CacheConcurrencyStrategy.SoftLock lock(Object key,
                                              Object version)
                                       throws CacheException
Stop any other transactions reading or writing this item to/from the cache. Send them straight to the database instead. (The lock does time out eventually.) This implementation tracks concurrent locks my transactions which simultaneously attempt to write to an item.

Specified by:
lock in interface CacheConcurrencyStrategy
Parameters:
key -
version -
Throws:
CacheException

put

public boolean put(Object key,
                   Object value,
                   long txTimestamp,
                   Object version,
                   Comparator versionComparator)
            throws CacheException
Do not add an item to the cache unless the current transaction timestamp is later than the timestamp at which the item was invalidated. (Otherwise, a stale item might be re-added if the database is operating in repeatable read isolation mode.)

Specified by:
put in interface CacheConcurrencyStrategy
Parameters:
key -
value -
txTimestamp - a timestamp prior to the transaction start time
version - the version number of the object we are putting
versionComparator - a Comparator to be used to compare version numbers
Returns:
true if the object was successfully cached
Throws:
CacheException

release

public void release(Object key,
                    CacheConcurrencyStrategy.SoftLock clientLock)
             throws CacheException
Release the soft lock on the item. Other transactions may now re-cache the item (assuming that no other transaction holds a simultaneous lock).

Specified by:
release in interface CacheConcurrencyStrategy
Parameters:
key -
Throws:
CacheException

clear

public void clear()
           throws CacheException
Description copied from interface: CacheConcurrencyStrategy
Evict all items from the cache immediately.

Specified by:
clear in interface CacheConcurrencyStrategy
Throws:
CacheException

remove

public void remove(Object key)
            throws CacheException
Description copied from interface: CacheConcurrencyStrategy
Evict an item from the cache immediately (without regard for transaction isolation).

Specified by:
remove in interface CacheConcurrencyStrategy
Parameters:
key -
Throws:
CacheException

destroy

public void destroy()
Description copied from interface: CacheConcurrencyStrategy
Clean up all resources.

Specified by:
destroy in interface CacheConcurrencyStrategy

afterUpdate

public void afterUpdate(Object key,
                        Object value,
                        Object version,
                        CacheConcurrencyStrategy.SoftLock clientLock)
                 throws CacheException
Re-cache the updated state, if and only if there there are no other concurrent soft locks. Release our lock.

Specified by:
afterUpdate in interface CacheConcurrencyStrategy
Throws:
CacheException

afterInsert

public void afterInsert(Object key,
                        Object value,
                        Object version)
                 throws CacheException
Add the new item to the cache, checking that no other transaction has accessed the item.

Specified by:
afterInsert in interface CacheConcurrencyStrategy
Throws:
CacheException

evict

public void evict(Object key)
           throws CacheException
Do nothing.

Specified by:
evict in interface CacheConcurrencyStrategy
Throws:
CacheException

insert

public void insert(Object key,
                   Object value)
            throws CacheException
Do nothing.

Specified by:
insert in interface CacheConcurrencyStrategy
Throws:
CacheException

update

public void update(Object key,
                   Object value)
            throws CacheException
Do nothing.

Specified by:
update in interface CacheConcurrencyStrategy
Throws:
CacheException

setMinimalPuts

public void setMinimalPuts(boolean minimalPuts)
                    throws HibernateException
Description copied from interface: CacheConcurrencyStrategy
Enable "minimal puts" mode for this cache

Specified by:
setMinimalPuts in interface CacheConcurrencyStrategy
Throws:
HibernateException