Enum Class CacheConcurrencyStrategy
- All Implemented Interfaces:
Serializable
,Comparable<CacheConcurrencyStrategy>
,java.lang.constant.Constable
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:
- read extremely frequently, and
- written infrequently.
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)
.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionIndicates that no concurrency strategy is specified, and that a default strategy should be used.Read/write access to the shared second-level cache with no locking.Read-only access to the shared second-level cache.Read/write access to the shared second-level cache using soft locks.Transactional access to the shared second-level cache. -
Method Summary
Modifier and TypeMethodDescriptionstatic CacheConcurrencyStrategy
fromAccessType
(AccessType accessType) Conversion fromAccessType
toCacheConcurrencyStrategy
.static CacheConcurrencyStrategy
Parse an external representation.Get theAccessType
corresponding to this concurrency strategy.static CacheConcurrencyStrategy
Returns the enum constant of this class with the specified name.static CacheConcurrencyStrategy[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
NONE
Indicates that no concurrency strategy is specified, and that a default strategy should be used. -
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:
-
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:
-
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:
-
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:
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-
toAccessType
Get theAccessType
corresponding to this concurrency strategy.- Returns:
- The corresponding concurrency strategy. Note that this will
return
null
forNONE
.
-
fromAccessType
Conversion fromAccessType
toCacheConcurrencyStrategy
.- Parameters:
accessType
- The access type to convert- Returns:
- The corresponding enum value.
NONE
is returned by default if unable to recognize theaccessType
or if theaccessType
isnull
.
-
parse
Parse an external representation.- Parameters:
name
- The external representation- Returns:
- The corresponding enum value, or
null
if no match was found.
-