Top Description Inners Fields Constructors Methods
org.hibernate.cache

public Class ReadWriteCache

extends Object
implements CacheConcurrencyStrategy
Class Inheritance
All Implemented Interfaces
org.hibernate.cache.CacheConcurrencyStrategy
Imports
java.io.Serializable, java.util.Comparator, org.slf4j.Logger, .LoggerFactory, org.hibernate.cache.access.SoftLock

Deprecated

as a consequence of CacheConcurrencyStrategy being deprecated. See corresponding docs for further information.

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 and Inner Type Summary

Modifier and TypeClass and Description
public static class
ReadWriteCache.Item

An item of cached data, timestamped with the time it was cached,.

public static class
ReadWriteCache.Lock

A soft lock which supports concurrent locking, timestamped with the time it was released

public static interface

Field Summary

Modifier and TypeField and Description
private Cache
cache

Deprecated as a consequence of Cache being deprecated.
private static final Logger
private int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public synchronized boolean
afterInsert(Object key, Object value, Object version)

Implements org.hibernate.cache.CacheConcurrencyStrategy.afterInsert.

Add the new item to the cache, checking that no other transaction has accessed the item.
public synchronized boolean
afterUpdate(Object key, Object value, Object version, SoftLock clientLock)

Implements org.hibernate.cache.CacheConcurrencyStrategy.afterUpdate.

Re-cache the updated state, if and only if there there are no other concurrent soft locks.
public void
clear()

Implements org.hibernate.cache.CacheConcurrencyStrategy.clear.

Evict all items from the cache immediately.
private void
decrementLock(Object key, ReadWriteCache.Lock lock)

decrement a lock and put it back in the cache

public void
public void
public synchronized Object
get(Object key, long
a timestamp prior to the transaction start time
txTimestamp
)

Implements org.hibernate.cache.CacheConcurrencyStrategy.get.

Do not return an item whose timestamp is later than the current transaction timestamp.
public Cache
getCache()

Implements org.hibernate.cache.CacheConcurrencyStrategy.getCache.

Deprecated as a consequence of Cache being deprecated.
Get the wrapped cache implementation
public String
pack-priv void
public boolean
insert(Object key, Object value, Object currentVersion)

Implements org.hibernate.cache.CacheConcurrencyStrategy.insert.

Do nothing.
private boolean
isUnlockable(SoftLock clientLock, ReadWriteCache.Lockable myLock)

Is the client's lock commensurate with the item in the cache?

public synchronized SoftLock
lock(Object key, Object version)

Implements org.hibernate.cache.CacheConcurrencyStrategy.lock.

Stop any other transactions reading or writing this item to/from the cache.
private int
nextLockId()

Generate an id for a new lock.

public synchronized boolean
put(Object key, Object value, long
a timestamp prior to the transaction start time
txTimestamp
,
Object
the item version number
version
,
Comparator<T>
a comparator used to compare version numbers
versionComparator
,
boolean
indicates that the cache should avoid a put is the item is already cached
minimalPut
)

Implements org.hibernate.cache.CacheConcurrencyStrategy.put.

Do not add an item to the cache unless the current transaction timestamp is later than the timestamp at which the item was invalidated.
public synchronized void
release(Object key, SoftLock clientLock)

Implements org.hibernate.cache.CacheConcurrencyStrategy.release.

Release the soft lock on the item.
public void
remove(Object key)

Implements org.hibernate.cache.CacheConcurrencyStrategy.remove.

Evict an item from the cache immediately (without regard for transaction isolation).
public void
setCache(Cache cache)

Implements org.hibernate.cache.CacheConcurrencyStrategy.setCache.

Deprecated as a consequence of Cache being deprecated.
Set the underlying cache implementation.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public boolean
update(Object key, Object value, Object currentVersion, Object previousVersion)

Implements org.hibernate.cache.CacheConcurrencyStrategy.update.

Do nothing.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

cacheback to summary
private Cache cache

Deprecated

as a consequence of Cache being deprecated. See corresponding docs for further information.

logback to summary
private static final Logger log
nextLockIdback to summary
private int nextLockId

Constructor Detail

ReadWriteCacheback to summary
public ReadWriteCache()

Method Detail

afterInsertback to summary
public synchronized boolean afterInsert(Object key, Object value, Object version) throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.afterInsert.

Add the new item to the cache, checking that no other transaction has accessed the item.

afterUpdateback to summary
public synchronized boolean afterUpdate(Object key, Object value, Object version, SoftLock clientLock) throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.afterUpdate.

Re-cache the updated state, if and only if there there are no other concurrent soft locks. Release our lock.

clearback to summary
public void clear() throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.clear.

Doc from org.hibernate.cache.CacheConcurrencyStrategy.clear.

Evict all items from the cache immediately.

decrementLockback to summary
private void decrementLock(Object key, ReadWriteCache.Lock lock) throws CacheException

decrement a lock and put it back in the cache

destroyback to summary
public void destroy()

Implements org.hibernate.cache.CacheConcurrencyStrategy.destroy.

Doc from org.hibernate.cache.CacheConcurrencyStrategy.destroy.

Clean up all resources.

evictback to summary
public void evict(Object key) throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.evict.

Do nothing.

getback to summary
public synchronized Object get(Object key, long txTimestamp) throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.get.

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.

Parameters
txTimestamp:long

Doc from org.hibernate.cache.CacheConcurrencyStrategy.get.

a timestamp prior to the transaction start time

Returns:Object

Doc from org.hibernate.cache.CacheConcurrencyStrategy.get.

the cached object or null

getCacheback to summary
public Cache getCache()

Implements org.hibernate.cache.CacheConcurrencyStrategy.getCache.

Doc from org.hibernate.cache.CacheConcurrencyStrategy.getCache.

Deprecated

as a consequence of Cache being deprecated. See corresponding docs for further information.

Get the wrapped cache implementation

getRegionNameback to summary
public String getRegionName()

Implements org.hibernate.cache.CacheConcurrencyStrategy.getRegionName.

Doc from org.hibernate.cache.CacheConcurrencyStrategy.getRegionName.

Get the cache region name

handleLockExpiryback to summary
pack-priv void handleLockExpiry(Object key) throws CacheException
insertback to summary
public boolean insert(Object key, Object value, Object currentVersion)

Implements org.hibernate.cache.CacheConcurrencyStrategy.insert.

Do nothing.

isUnlockableback to summary
private boolean isUnlockable(SoftLock clientLock, ReadWriteCache.Lockable myLock) throws CacheException

Is the client's lock commensurate with the item in the cache? If it is not, we know that the cache expired the original lock.

lockback to summary
public synchronized SoftLock lock(Object key, Object version) throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.lock.

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 of transactions which simultaneously attempt to write to an item.

nextLockIdback to summary
private int nextLockId()

Generate an id for a new lock. Uniqueness per cache instance is very desirable but not absolutely critical. Must be called from one of the synchronized methods of this class.

putback to summary
public synchronized boolean put(Object key, Object value, long txTimestamp, Object version, Comparator<T> versionComparator, boolean minimalPut) throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.put.

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.) For versioned data, don't add the item unless it is the later version.

Parameters
txTimestamp:long

Doc from org.hibernate.cache.CacheConcurrencyStrategy.put.

a timestamp prior to the transaction start time

version:Object

Doc from org.hibernate.cache.CacheConcurrencyStrategy.put.

the item version number

versionComparator:Comparator<T>

Doc from org.hibernate.cache.CacheConcurrencyStrategy.put.

a comparator used to compare version numbers

minimalPut:boolean

Doc from org.hibernate.cache.CacheConcurrencyStrategy.put.

indicates that the cache should avoid a put is the item is already cached

Returns:boolean

Doc from org.hibernate.cache.CacheConcurrencyStrategy.put.

true if the object was successfully cached

releaseback to summary
public synchronized void release(Object key, SoftLock clientLock) throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.release.

Release the soft lock on the item. Other transactions may now re-cache the item (assuming that no other transaction holds a simultaneous lock).

removeback to summary
public void remove(Object key) throws CacheException

Implements org.hibernate.cache.CacheConcurrencyStrategy.remove.

Doc from org.hibernate.cache.CacheConcurrencyStrategy.remove.

Evict an item from the cache immediately (without regard for transaction isolation).

setCacheback to summary
public void setCache(Cache cache)

Implements org.hibernate.cache.CacheConcurrencyStrategy.setCache.

Doc from org.hibernate.cache.CacheConcurrencyStrategy.setCache.

Deprecated

as a consequence of Cache being deprecated. See corresponding docs for further information.

Set the underlying cache implementation.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

updateback to summary
public boolean update(Object key, Object value, Object currentVersion, Object previousVersion)

Implements org.hibernate.cache.CacheConcurrencyStrategy.update.

Do nothing.

org.hibernate.cache back to summary

public final Class ReadWriteCache.Item

extends Object
implements Serializable, Lockable
Class Inheritance
All Implemented Interfaces
org.hibernate.cache.ReadWriteCache.Lockable, java.io.Serializable

An item of cached data, timestamped with the time it was cached,.
See Also
ReadWriteCache

Field Summary

Modifier and TypeField and Description
private final long
private final Object
private final Object

Constructor Summary

AccessConstructor and Description
public
Item(Object value, Object version, long currentTimestamp)

Method Summary

Modifier and TypeMethod and Description
public long
getFreshTimestamp()

The timestamp on the cached data

public Object
getValue()

The actual cached data

public boolean
isGettable(long txTimestamp)

Implements org.hibernate.cache.ReadWriteCache.Lockable.isGettable.

Is this item visible to the timestamped transaction?
public boolean
public boolean
isPuttable(long txTimestamp, Object newVersion, Comparator<T> comparator)

Implements org.hibernate.cache.ReadWriteCache.Lockable.isPuttable.

Don't overwite already cached items
public ReadWriteCache.Lock
lock(long timeout, int id)

Implements org.hibernate.cache.ReadWriteCache.Lockable.lock.

Lock the item
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

freshTimestampback to summary
private final long freshTimestamp
valueback to summary
private final Object value
versionback to summary
private final Object version

Constructor Detail

Itemback to summary
public Item(Object value, Object version, long currentTimestamp)

Method Detail

getFreshTimestampback to summary
public long getFreshTimestamp()

The timestamp on the cached data

getValueback to summary
public Object getValue()

The actual cached data

isGettableback to summary
public boolean isGettable(long txTimestamp)

Implements org.hibernate.cache.ReadWriteCache.Lockable.isGettable.

Is this item visible to the timestamped transaction?

isLockback to summary
public boolean isLock()

Implements org.hibernate.cache.ReadWriteCache.Lockable.isLock.

Not a lock!

isPuttableback to summary
public boolean isPuttable(long txTimestamp, Object newVersion, Comparator<T> comparator)

Implements org.hibernate.cache.ReadWriteCache.Lockable.isPuttable.

Don't overwite already cached items

lockback to summary
public ReadWriteCache.Lock lock(long timeout, int id)

Implements org.hibernate.cache.ReadWriteCache.Lockable.lock.

Lock the item

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

org.hibernate.cache back to summary

public final Class ReadWriteCache.Lock

extends Object
implements Serializable, Lockable, SoftLock
Class Inheritance
All Implemented Interfaces
org.hibernate.cache.access.SoftLock, org.hibernate.cache.ReadWriteCache.Lockable, java.io.Serializable

A soft lock which supports concurrent locking, timestamped with the time it was released
Author
Gavin King

Field Summary

Modifier and TypeField and Description
private boolean
private final int
private int
private long
private long
private final Object

Constructor Summary

AccessConstructor and Description
public
Lock(long timeout, int id, Object version)

Method Summary

Modifier and TypeMethod and Description
public int
public long
public boolean
isGettable(long txTimestamp)

Implements org.hibernate.cache.ReadWriteCache.Lockable.isGettable.

locks are not returned to the client!
public boolean
public boolean
isPuttable(long txTimestamp, Object newVersion, Comparator<T> comparator)

Implements org.hibernate.cache.ReadWriteCache.Lockable.isPuttable.

Can the timestamped transaction re-cache this locked item now?
public ReadWriteCache.Lock
lock(long timeout, int id)

Implements org.hibernate.cache.ReadWriteCache.Lockable.lock.

Increment the lock, setting the new lock timeout
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public void
unlock(long currentTimestamp)

Decrement the lock, setting the unlock timestamp if now unlocked

public boolean
wasLockedConcurrently()

Was this lock held concurrently by multiple transactions?

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

concurrentLockback to summary
private boolean concurrentLock
idback to summary
private final int id
multiplicityback to summary
private int multiplicity
timeoutback to summary
private long timeout
unlockTimestampback to summary
private long unlockTimestamp
versionback to summary
private final Object version

Constructor Detail

Lockback to summary
public Lock(long timeout, int id, Object version)

Method Detail

getIdback to summary
public int getId()
getUnlockTimestampback to summary
public long getUnlockTimestamp()
isGettableback to summary
public boolean isGettable(long txTimestamp)

Implements org.hibernate.cache.ReadWriteCache.Lockable.isGettable.

locks are not returned to the client!

isLockback to summary
public boolean isLock()

Implements org.hibernate.cache.ReadWriteCache.Lockable.isLock.

Yes, this is a lock

isPuttableback to summary
public boolean isPuttable(long txTimestamp, Object newVersion, Comparator<T> comparator)

Implements org.hibernate.cache.ReadWriteCache.Lockable.isPuttable.

Can the timestamped transaction re-cache this locked item now?

lockback to summary
public ReadWriteCache.Lock lock(long timeout, int id)

Implements org.hibernate.cache.ReadWriteCache.Lockable.lock.

Increment the lock, setting the new lock timeout

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

unlockback to summary
public void unlock(long currentTimestamp)

Decrement the lock, setting the unlock timestamp if now unlocked

wasLockedConcurrentlyback to summary
public boolean wasLockedConcurrently()

Was this lock held concurrently by multiple transactions?

org.hibernate.cache back to summary

public Interface ReadWriteCache.Lockable

Known Direct Implementers
org.hibernate.cache.ReadWriteCache.Item, org.hibernate.cache.ReadWriteCache.Lock

Method Summary

Modifier and TypeMethod and Description
public boolean
isGettable(long txTimestamp)

public boolean
public boolean
isPuttable(long txTimestamp, Object newVersion, Comparator<T> comparator)

public ReadWriteCache.Lock
lock(long timeout, int id)

Method Detail

isGettableback to summary
public boolean isGettable(long txTimestamp)
isLockback to summary
public boolean isLock()
isPuttableback to summary
public boolean isPuttable(long txTimestamp, Object newVersion, Comparator<T> comparator)
lockback to summary
public ReadWriteCache.Lock lock(long timeout, int id)