Class TransactionAwareSingleton<T>

java.lang.Object
org.alfresco.repo.transaction.TransactionListenerAdapter
org.alfresco.repo.transaction.TransactionAwareSingleton<T>
All Implemented Interfaces:
TransactionListener, org.alfresco.util.transaction.TransactionListener

public class TransactionAwareSingleton<T> extends TransactionListenerAdapter
A transactionally-safe storage class for singleton objects. Changes to the singleton are only visibly promoted when the transaction is committed.

private static final TransactionAwareSingleton MY_SINGLETON = new TransactionAwareSingleton();

All modifications to the singleton via get() and put(Object) are made in a transaction-local manner and promoted to the shared value in a thread-safe manner upon transacton completion. Transaction-local changes take precedence over the shared value.

Author:
Derek Hulley
See Also:
  • Constructor Details

    • TransactionAwareSingleton

      public TransactionAwareSingleton()
  • Method Details

    • get

      public T get()
      Returns:
      Returns the transaction- and thread-safe wrapped instance
    • put

      public void put(T value)
      Store the value in a transaction- and thread-safe manner. It will only be persisted at the end of the transaction but will be visible to the current transaction from this call onwards.
      Parameters:
      value - the value to store
    • afterCommit

      public void afterCommit()
      Promotes the storage value to the single value, if required
      Specified by:
      afterCommit in interface TransactionListener
      Specified by:
      afterCommit in interface org.alfresco.util.transaction.TransactionListener
      Overrides:
      afterCommit in class TransactionListenerAdapter