Top Description Fields Constructors Methods
org.hibernate.annotations

public final Enum CacheConcurrencyStrategy

extends Enum<CacheConcurrencyStrategy>
Class Inheritance
Imports
org.hibernate.AssertionFailure, org.hibernate.cache.spi.access.AccessType

Identifies policies for managing concurrent access to the shared second-level cache.

A second-level cache is shared between all concurrent active sessions created by a given session factory. The cache shares state between transactions, while bypassing the database's locking or multi-version concurrency control. This tends to undermine the ACID properties of transaction processing, which are only guaranteed when all sharing of data is mediated by the database.

Of course, as a general rule, the only sort of data that really belongs in a second-level cache is data that is both:

When an entity or collection is marked cacheable, it must indicate the policy which governs concurrent access to its second-level cache, by selecting a CacheConcurrencyStrategy appropriate to the expected patterns of data access. The most important consideration is the frequency of updates which mutate the state of the cached entity or collection.

For example, if an entity is immutable, READ_ONLY is the most appropriate policy, and the entity should be annotated @Cache(usage=READ_ONLY).

Author
Emmanuel Bernard
See Also
The corresponding SPI.

Field Summary

Modifier and TypeField and Description
public static final CacheConcurrencyStrategy
NONE

Indicates that no concurrency strategy is specified, and that a default strategy should be used.

public static final CacheConcurrencyStrategy
NONSTRICT_READ_WRITE

Read/write access to the shared second-level cache with no locking.

public static final CacheConcurrencyStrategy
READ_ONLY

Read-only access to the shared second-level cache.

public static final CacheConcurrencyStrategy
READ_WRITE

Read/write access to the shared second-level cache using soft locks.

public static final CacheConcurrencyStrategy
TRANSACTIONAL

Transactional access to the shared second-level cache.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static CacheConcurrencyStrategy

Returns:

The corresponding enum value. NONE is returned by default if unable to recognize the accessType or if the accessType is null.
fromAccessType
(AccessType
The access type to convert
accessType
)

Conversion from AccessType to CacheConcurrencyStrategy.

private boolean
public static CacheConcurrencyStrategy

Returns:

The corresponding enum value, or null if no match was found.
parse
(String
The external representation
name
)

Parse an external representation.

public AccessType

Returns:

The corresponding concurrency strategy. Note that this will return null for NONE.
toAccessType
()

Get the AccessType corresponding to this concurrency strategy.

public static CacheConcurrencyStrategy
public static CacheConcurrencyStrategy[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

NONEback to summary
public static final CacheConcurrencyStrategy NONE

Indicates that no concurrency strategy is specified, and that a default strategy should be used.

See Also
org.hibernate.cfg.AvailableSettings#DEFAULT_CACHE_CONCURRENCY_STRATEGY
NONSTRICT_READ_WRITEback to summary
public static final CacheConcurrencyStrategy NONSTRICT_READ_WRITE

Read/write access to the shared second-level cache with no locking.

Indicates that the cached object is sometimes updated, but that it is extremely unlikely that two transactions will attempt to update the same item of data at the same time. This strategy does not use locks. When an item is updated, the cache is invalidated both before and after completion of the updating transaction. But without locking, it's impossible to completely rule out the possibility of a second transaction storing or retrieving stale data in or from the cache during the completion process of the first transaction.

This concurrency strategy is not compatible with serializable transaction isolation.

See Also
AccessType#NONSTRICT_READ_WRITE
READ_ONLYback to summary
public static final CacheConcurrencyStrategy READ_ONLY

Read-only access to the shared second-level cache.

Indicates that the cached object is immutable, and is never updated. If an entity with this cache concurrency is updated, an exception is thrown. This is the simplest, safest, and best-performing cache concurrency strategy. It's particularly suitable for so-called "reference" data.

See Also
AccessType#READ_ONLY
READ_WRITEback to summary
public static final CacheConcurrencyStrategy READ_WRITE

Read/write access to the shared second-level cache using soft locks.

Indicates a non-vanishing likelihood that two concurrent transactions attempt to update the same item of data simultaneously. This strategy uses "soft" locks to prevent concurrent transactions from retrieving or storing a stale item from or in the cache during the transaction completion process. A soft lock is simply a marker entry placed in the cache while the updating transaction completes.

  • A second transaction may not read the item from the cache while the soft lock is present, and instead simply proceeds to read the item directly from the database, exactly as if a regular cache miss had occurred.
  • Similarly, the soft lock also prevents this second transaction from storing a stale item to the cache when it returns from its round trip to the database with something that might not quite be the latest version.

This concurrency strategy is not compatible with serializable transaction isolation.

See Also
AccessType#READ_WRITE
TRANSACTIONALback to summary
public static final CacheConcurrencyStrategy TRANSACTIONAL

Transactional access to the shared second-level cache.

Indicates that concurrent writes are common, and the only way to maintain synchronization between the second-level cache and the database is via the use of a fully transactional cache provider. In this case, the cache and the database must cooperate via JTA or the XA protocol, and Hibernate itself takes on little responsibility for maintaining the integrity of the cache.

See Also
AccessType#TRANSACTIONAL

Constructor Detail

CacheConcurrencyStrategyback to summary
private CacheConcurrencyStrategy()

Method Detail

fromAccessTypeback to summary
public static CacheConcurrencyStrategy fromAccessType(AccessType accessType)

Conversion from AccessType to CacheConcurrencyStrategy.

Parameters
accessType:AccessType

The access type to convert

Returns:CacheConcurrencyStrategy

The corresponding enum value. NONE is returned by default if unable to recognize the accessType or if the accessType is null.

isMatchback to summary
private boolean isMatch(String name)
parseback to summary
public static CacheConcurrencyStrategy parse(String name)

Parse an external representation.

Parameters
name:String

The external representation

Returns:CacheConcurrencyStrategy

The corresponding enum value, or null if no match was found.

toAccessTypeback to summary
public AccessType toAccessType()

Get the AccessType corresponding to this concurrency strategy.

Returns:AccessType

The corresponding concurrency strategy. Note that this will return null for NONE.

valueOfback to summary
public static CacheConcurrencyStrategy valueOf(String name)
valuesback to summary
public static CacheConcurrencyStrategy[] values()