Class TxnThreadLocal

java.lang.Object
org.multiverse.api.TxnThreadLocal

public final class TxnThreadLocal extends Object
A ThreadLocal that contains the current Txn. The Stm and the Txn should not rely on threadlocals, they are only used for convenience to reduce the need to carry around a Txn.

This TxnThreadLocal has an optimization that prevents accessing the threadlocal too many times. The Container wraps the Txn, so if a Thread gets a reference to that container and holds it, it can modify the current transaction with a direct field access instead of another threadlocal access. It should be used with extreme care, because the Container should not leak to another thread. It is very useful for the TxnExecutor for example because a get/getAndSet/clear needs to be called otherwise.

  • Field Details

  • Constructor Details

    • TxnThreadLocal

      private TxnThreadLocal()
  • Method Details

    • getThreadLocalTxn

      public static Txn getThreadLocalTxn()
      Gets the threadlocal Txn. If no transaction is set, null is returned.

      No checks are done on the state of the transaction (so it could be that an aborted or committed transaction is returned).

      Returns:
      the threadlocal transaction.
    • getThreadLocalTxnContainer

      public static TxnThreadLocal.Container getThreadLocalTxnContainer()
      ThreadLocalTransaction Gets the ThreadLocal container that stores the Txn. Use this with extreme care because the Container should not leak to another thread. It is purely means as a performance optimization to prevent repeated (expensive) threadlocal access, and replace it by a cheap field access.
      Returns:
      the Container. The returned value will never be null.
    • getRequiredThreadLocalTxn

      public static Txn getRequiredThreadLocalTxn()
      Gets the threadlocal Txn or throws a TxnMandatoryException if no transaction is found.

      No checks are done on the state of the transaction (so it could be that an aborted or committed transaction is returned).

      Returns:
      the threadlocal transaction.
      Throws:
      TxnMandatoryException - if no thread local transaction is found.
    • clearThreadLocalTxn

      public static void clearThreadLocalTxn()
      Clears the threadlocal transaction.

      If a transaction is available, it isn't aborted or committed.

    • setThreadLocalTxn

      public static void setThreadLocalTxn(Txn txn)
      Sets the threadlocal transaction. The transaction is allowed to be null, effectively clearing the current thread local transaction.

      If a transaction is available, it isn't aborted or committed.

      Parameters:
      txn - the new thread local transaction.