FileDocCategorySizeDatePackage
CacheConcurrencyStrategy.javaAPI DocHibernate 3.2.56096Thu Feb 09 13:48:44 GMT 2006org.hibernate.cache

CacheConcurrencyStrategy

public interface CacheConcurrencyStrategy
Implementors manage transactional access to cached data. Transactions pass in a timestamp indicating transaction start time. Two different implementation patterns are provided for.
  • A transaction-aware cache implementation might be wrapped by a "synchronous" concurrency strategy, where updates to the cache are written to the cache inside the transaction.
  • A non transaction-aware cache would be wrapped by an "asynchronous" concurrency strategy, where items are merely "soft locked" during the transaction and then updated during the "after transaction completion" phase; the soft lock is not an actual lock on the database row - only upon the cached representation of the item.

In terms of entity caches, the expected call sequences are:

  • DELETES : {@link #lock} -> {@link #evict} -> {@link #release}
  • UPDATES : {@link #lock} -> {@link #update} -> {@link #afterUpdate}
  • INSERTS : {@link #insert} -> {@link #afterInsert}

In terms of collection caches, all modification actions actually just invalidate the entry(s). The call sequence here is: {@link #lock} -> {@link #evict} -> {@link #release}

Note that, for an asynchronous cache, cache invalidation must be a two step process (lock->release, or lock-afterUpdate), since this is the only way to guarantee consistency with the database for a nontransactional cache implementation. For a synchronous cache, cache invalidation is a single step process (evict, or update). Hence, this interface defines a three step process, to cater for both models.

Note that query result caching does not go through a concurrency strategy; they are managed directly against the underlying {@link Cache cache regions}.

Fields Summary
Constructors Summary
Methods Summary
public booleanafterInsert(java.lang.Object key, java.lang.Object value, java.lang.Object version)
Called after an item has been inserted (after the transaction completes), instead of calling release(). This method is used by "asynchronous" concurrency strategies.

public booleanafterUpdate(java.lang.Object key, java.lang.Object value, java.lang.Object version, org.hibernate.cache.CacheConcurrencyStrategy$SoftLock lock)
Called after an item has been updated (after the transaction completes), instead of calling release(). This method is used by "asynchronous" concurrency strategies.

public voidclear()
Evict all items from the cache immediately.

throws
CacheException

public voiddestroy()
Clean up all resources.

public voidevict(java.lang.Object key)
Called after an item has become stale (before the transaction completes). This method is used by "synchronous" concurrency strategies.

public java.lang.Objectget(java.lang.Object key, long txTimestamp)
Attempt to retrieve an object from the cache. Mainly used in attempting to resolve entities/collections from the second level cache.

param
key
param
txTimestamp a timestamp prior to the transaction start time
return
the cached object or null
throws
CacheException

public CachegetCache()
Get the wrapped cache implementation

public java.lang.StringgetRegionName()
Get the cache region name

public booleaninsert(java.lang.Object key, java.lang.Object value, java.lang.Object currentVersion)
Called after an item has been inserted (before the transaction completes), instead of calling evict(). This method is used by "synchronous" concurrency strategies.

public org.hibernate.cache.CacheConcurrencyStrategy$SoftLocklock(java.lang.Object key, java.lang.Object version)
We are going to attempt to update/delete the keyed object. This method is used by "asynchronous" concurrency strategies.

The returned object must be passed back to release(), to release the lock. Concurrency strategies which do not support client-visible locks may silently return null.

param
key
param
version
throws
CacheException

public booleanput(java.lang.Object key, java.lang.Object value, long txTimestamp, java.lang.Object version, java.util.Comparator versionComparator, boolean minimalPut)
Attempt to cache an object, after loading from the database.

param
key
param
value
param
txTimestamp a timestamp prior to the transaction start time
param
version the item version number
param
versionComparator a comparator used to compare version numbers
param
minimalPut indicates that the cache should avoid a put is the item is already cached
return
true if the object was successfully cached
throws
CacheException

public voidrelease(java.lang.Object key, org.hibernate.cache.CacheConcurrencyStrategy$SoftLock lock)
Called when we have finished the attempted update/delete (which may or may not have been successful), after transaction completion. This method is used by "asynchronous" concurrency strategies.

param
key
throws
CacheException

public voidremove(java.lang.Object key)
Evict an item from the cache immediately (without regard for transaction isolation).

param
key
throws
CacheException

public voidsetCache(Cache cache)
Set the underlying cache implementation.

param
cache

public booleanupdate(java.lang.Object key, java.lang.Object value, java.lang.Object currentVersion, java.lang.Object previousVersion)
Called after an item has been updated (before the transaction completes), instead of calling evict(). This method is used by "synchronous" concurrency strategies.