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).
The corresponding SPI.
| Modifier and Type | Field 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. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method 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.The access type to convert accessType)Conversion from |
| private boolean | |
| public static CacheConcurrencyStrategy | |
| public AccessType | |
| public static CacheConcurrencyStrategy | |
| public static CacheConcurrencyStrategy[] |
| NONE | back to summary |
|---|---|
| public static final CacheConcurrencyStrategy NONE Indicates that no concurrency strategy is specified, and that a default strategy should be used. | |
| NONSTRICT_READ_WRITE | back 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.
| |
| READ_ONLY | back 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.
| |
| READ_WRITE | back 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.
This concurrency strategy is not compatible with serializable transaction isolation.
| |
| TRANSACTIONAL | back 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.
| |
| CacheConcurrencyStrategy | back to summary |
|---|---|
| private CacheConcurrencyStrategy() | |
| fromAccessType | back to summary |
|---|---|
| public static CacheConcurrencyStrategy fromAccessType(AccessType accessType) Conversion from
| |
| isMatch | back to summary |
|---|---|
| private boolean isMatch(String name) | |
| parse | back to summary |
|---|---|
| public static CacheConcurrencyStrategy parse(String name) Parse an external representation.
| |
| toAccessType | back to summary |
|---|---|
| public AccessType toAccessType() Get the
| |
| valueOf | back to summary |
|---|---|
| public static CacheConcurrencyStrategy valueOf(String name) | |
| values | back to summary |
|---|---|
| public static CacheConcurrencyStrategy[] values() | |