Class TransactionalCompositeConfiguration

All Implemented Interfaces:
Transactional, Cloneable, Configuration, EventSource, ImmutableConfiguration, SynchronizerSupport

public class TransactionalCompositeConfiguration extends CompositeConfiguration implements Transactional
A CompositeConfiguration of which a thread can group the accesses into a transaction. A read-only transaction holds the read lock of this composite, so that no other thread writes it; a writable transaction holds the write lock, so that no other thread reads or writes it. The lock is that of the Synchronizer of this composite, which therefore has to lock, as the ReadWriteSynchronizer set by the ConfigurationManager does. Since a read lock cannot be upgraded, writing or starting a writable level within a read-only transaction is refused instead of waiting forever.

If the in-memory configuration supports transactions, a transaction of this composite starts one there, which stores the changed values; otherwise the transaction only locks. The other child configurations do not take part: setting or removing a value removes it from each of them only when the outermost level of the transaction is finished, after the in-memory configuration has committed, so that rolling back the transaction leaves them unchanged.

A single setProperty or clearProperty runs within a transaction, joining one of the calling thread, so that rolling back after a failure restores the old value of the in-memory configuration. Outside a transaction, its own level is the outermost one, so it removes the value from the other child configurations when it finishes and leaves them unchanged if it fails. A subset of this composite sets and clears a value through this composite, so this holds for the subset too.

Unlike the library's composite, this one hands a value to the in-memory configuration as it is, a collection as a whole, so that the in-memory configuration decides how to store it. A subset of this composite adds a value through this composite, so that it is handed over as a whole too.

  • Constructor Details

    • TransactionalCompositeConfiguration

      public TransactionalCompositeConfiguration(Configuration inMemoryConfiguration)
      Creates a new composite configuration having the designated in-memory configuration, which stores the values changed through this composite.
      Parameters:
      inMemoryConfiguration - The configuration which stores the values changed through this composite.
  • Method Details

    • startTransaction

      public void startTransaction(boolean readOnly)
      Description copied from interface: Transactional
      Starts a transaction of the calling thread on this object. If the calling thread has started one already, this joins it and opens another level. A failed start leaves no level behind.
      Specified by:
      startTransaction in interface Transactional
      Parameters:
      readOnly - Whether the transaction only reads values. This is set by the outermost start; a nested read-only level joins a writable transaction.
    • finishTransaction

      public void finishTransaction()
      Description copied from interface: Transactional
      Finishes the current level of the transaction of the calling thread. Finishing the outermost level commits the transaction, finishing a nested level only ends that level. The level still has to be closed. If committing fails, the outermost level remains unfinished, so that closing it rolls the transaction back.
      Specified by:
      finishTransaction in interface Transactional
    • closeTransaction

      public void closeTransaction()
      Description copied from interface: Transactional
      Closes the current level of the transaction of the calling thread. Closing the outermost level rolls the transaction back unless it has been finished, and releases what the transaction holds. This belongs in a finally block. So as not to hide the exception which skipped finishing, a failure when rolling back or releasing is logged instead of thrown.
      Specified by:
      closeTransaction in interface Transactional
    • clone

      public Object clone()
      Refuses to clone this composite: a copy would share the transactions of the threads with it.
      Overrides:
      clone in class CompositeConfiguration
      Returns:
      Nothing, this always throws.
      Throws:
      ConfigurationRuntimeException - Since this composite cannot be cloned, a ConfigurationRuntimeException wrapping a CloneNotSupportedException will always be thrown.
    • addPropertyInternal

      protected void addPropertyInternal(String key, Object value)
      Overrides:
      addPropertyInternal in class AbstractConfiguration
    • setPropertyInternal

      protected void setPropertyInternal(String key, Object value)
      Overrides:
      setPropertyInternal in class AbstractConfiguration
    • clearPropertyDirect

      protected void clearPropertyDirect(String key)
      Overrides:
      clearPropertyDirect in class CompositeConfiguration
    • subset

      public Configuration subset(String prefix)
      Specified by:
      subset in interface Configuration
      Overrides:
      subset in class AbstractConfiguration
    • beginWrite

      protected void beginWrite(boolean optimize)
      Overrides:
      beginWrite in class AbstractConfiguration