Class TxnThreadLocal
- java.lang.Object
-
- org.multiverse.api.TxnThreadLocal
-
public final class TxnThreadLocal extends Object
AThreadLocal
that contains the currentTxn
. TheStm
and theTxn
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TxnThreadLocal.Container
-
Field Summary
Fields Modifier and Type Field Description static ThreadLocal<TxnThreadLocal.Container>
threadlocal
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
clearThreadLocalTxn()
Clears the threadlocal transaction.static Txn
getRequiredThreadLocalTxn()
Gets the threadlocalTxn
or throws aTxnMandatoryException
if no transaction is found.static Txn
getThreadLocalTxn()
Gets the threadlocalTxn
.static TxnThreadLocal.Container
getThreadLocalTxnContainer()
ThreadLocalTransaction Gets the ThreadLocal container that stores the Txn.static void
setThreadLocalTxn(Txn txn)
Sets the threadlocal transaction.
-
-
-
Field Detail
-
threadlocal
public static final ThreadLocal<TxnThreadLocal.Container> threadlocal
-
-
Method Detail
-
getThreadLocalTxn
public static Txn getThreadLocalTxn()
Gets the threadlocalTxn
. 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 threadlocalTxn
or throws aTxnMandatoryException
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.
-
-