Package org.hibernate.cache
Class ReadWriteCache
java.lang.Object
org.hibernate.cache.ReadWriteCache
- All Implemented Interfaces:
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.
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAn item of cached data, timestamped with the time it was cached,.static final classA soft lock which supports concurrent locking, timestamped with the time it was releasedstatic interface -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanafterInsert(Object key, Object value, Object version) Add the new item to the cache, checking that no other transaction has accessed the item.booleanafterUpdate(Object key, Object value, Object version, SoftLock clientLock) Re-cache the updated state, if and only if there there are no other concurrent soft locks.voidclear()Evict all items from the cache immediately.voiddestroy()Clean up all resources.voidDo nothing.Do not return an item whose timestamp is later than the current transaction timestamp.getCache()Get the wrapped cache implementationGet the cache region namebooleanDo nothing.Stop any other transactions reading or writing this item to/from the cache.booleanput(Object key, Object value, long txTimestamp, Object version, Comparator versionComparator, boolean minimalPut) Do not add an item to the cache unless the current transaction timestamp is later than the timestamp at which the item was invalidated.voidRelease the soft lock on the item.voidEvict an item from the cache immediately (without regard for transaction isolation).voidSet the underlying cache implementation.toString()booleanDo nothing.
-
Constructor Details
-
ReadWriteCache
public ReadWriteCache()
-
-
Method Details
-
setCache
Description copied from interface:CacheConcurrencyStrategySet the underlying cache implementation.- Specified by:
setCachein interfaceCacheConcurrencyStrategy- Parameters:
cache-
-
getCache
Description copied from interface:CacheConcurrencyStrategyGet the wrapped cache implementation- Specified by:
getCachein interfaceCacheConcurrencyStrategy
-
getRegionName
Description copied from interface:CacheConcurrencyStrategyGet the cache region name- Specified by:
getRegionNamein interfaceCacheConcurrencyStrategy
-
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.- Specified by:
getin interfaceCacheConcurrencyStrategy- Parameters:
key-txTimestamp- a timestamp prior to the transaction start time- Returns:
- the cached object or null
- Throws:
CacheException
-
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.- Specified by:
lockin interfaceCacheConcurrencyStrategy- Parameters:
key-version-- Throws:
CacheException
-
put
public boolean put(Object key, Object value, long txTimestamp, Object version, Comparator versionComparator, boolean minimalPut) 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.) For versioned data, don't add the item unless it is the later version.- Specified by:
putin interfaceCacheConcurrencyStrategy- Parameters:
key-value-txTimestamp- a timestamp prior to the transaction start timeversion- the item version numberversionComparator- a comparator used to compare version numbersminimalPut- indicates that the cache should avoid a put is the item is already cached- Returns:
- true if the object was successfully cached
- Throws:
CacheException
-
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).- Specified by:
releasein interfaceCacheConcurrencyStrategy- Parameters:
key-- Throws:
CacheException
-
clear
Description copied from interface:CacheConcurrencyStrategyEvict all items from the cache immediately.- Specified by:
clearin interfaceCacheConcurrencyStrategy- Throws:
CacheException
-
remove
Description copied from interface:CacheConcurrencyStrategyEvict an item from the cache immediately (without regard for transaction isolation).- Specified by:
removein interfaceCacheConcurrencyStrategy- Parameters:
key-- Throws:
CacheException
-
destroy
public void destroy()Description copied from interface:CacheConcurrencyStrategyClean up all resources.- Specified by:
destroyin interfaceCacheConcurrencyStrategy
-
afterUpdate
public boolean afterUpdate(Object key, Object value, Object version, 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:
afterUpdatein interfaceCacheConcurrencyStrategy- Throws:
CacheException
-
afterInsert
Add the new item to the cache, checking that no other transaction has accessed the item.- Specified by:
afterInsertin interfaceCacheConcurrencyStrategy- Throws:
CacheException
-
evict
Do nothing.- Specified by:
evictin interfaceCacheConcurrencyStrategy- Throws:
CacheException
-
insert
Do nothing.- Specified by:
insertin interfaceCacheConcurrencyStrategy
-
update
Do nothing.- Specified by:
updatein interfaceCacheConcurrencyStrategy
-
toString
-