Enum CacheConcurrencyStrategy

  • All Implemented Interfaces:
    Serializable, Comparable<CacheConcurrencyStrategy>

    public enum CacheConcurrencyStrategy
    extends Enum<CacheConcurrencyStrategy>
    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:

    • 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:
    The corresponding SPI.
    • Enum Constant Detail

      • READ_ONLY

        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:

        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:
      • READ_WRITE

        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:

        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:
    • Method Detail

      • values

        public static CacheConcurrencyStrategy[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (CacheConcurrencyStrategy c : CacheConcurrencyStrategy.values())
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static CacheConcurrencyStrategy valueOf​(String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        name - the name of the enum constant to be returned.
        the enum constant with the specified name
        IllegalArgumentException - if this enum type has no constant with the specified name
        NullPointerException - if the argument is null
      • toAccessType

        public AccessType toAccessType()
        Get the AccessType corresponding to this concurrency strategy.
        The corresponding concurrency strategy. Note that this will return null for NONE.
      • parse

        public static CacheConcurrencyStrategy parse​(String name)
        Parse an external representation.
        name - The external representation
        The corresponding enum value, or null if no match was found.