Supports one resource per key without overwriting, that is, a resource needs to be removed before a new one can be set for the same key. Supports a list of transaction synchronizations if synchronization is active.
Resource management code should check for thread-bound resources, e.g. JDBC
Connections or Hibernate Sessions, via getResource. Such code is
normally not supposed to bind resources to threads, as this is the responsibility
of transaction managers. A further option is to lazily bind on first use if
transaction synchronization is active, for performing transactions that span
an arbitrary number of resources.
Transaction synchronization must be activated and deactivated by a transaction
manager via initSynchronization() and clearSynchronization().
This is automatically supported by AbstractPlatformTransactionManager,
and thus by all standard Spring transaction managers, such as
org. and
org..
Resource management code should only register synchronizations when this
manager is active, which can be checked via isSynchronizationActive;
it should perform immediate resource cleanup else. If transaction synchronization
isn't active, there is either no current transaction, or the transaction manager
doesn't support transaction synchronization.
Synchronization is for example used to always return the same resources within a JTA transaction, e.g. a JDBC Connection or a Hibernate Session for any given DataSource or SessionFactory, respectively.
isSynchronizationActive, registerSynchronization, TransactionSynchronization, AbstractPlatformTransactionManager#setTransactionSynchronization, org.springframework.transaction.jta.JtaTransactionManager , org.springframework.jdbc.datasource.DataSourceTransactionManager , org.springframework.jdbc.datasource.DataSourceUtils#getConnection
| Modifier and Type | Field and Description |
|---|---|
| private static final ThreadLocal | |
| private static final ThreadLocal | |
| private static final ThreadLocal | |
| private static final ThreadLocal | |
| private static final ThreadLocal | |
| private static final ThreadLocal |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static void | bindResource(Object
the key to bind the value to (usually the resource factory) key, Object the value to bind (usually the active resource object) value)Bind the given resource for the given key to the current thread. |
| public static void | clear()
Clear the entire transaction synchronization state for the current thread: registered synchronizations as well as the various transaction characteristics. |
| public static void | |
| private static Object | doGetResource(Object actualKey)
Actually check the value of the resource that is bound for the given key. |
| private static Object | doUnbindResource(Object actualKey)
Actually remove the value of the resource that is bound for the given key. |
| public static Integer | Returns: the currently exposed isolation level, according to the JDBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), ornull if noneReturn the isolation level for the current transaction, if any. |
| public static String | |
| public static Object | Returns: a value bound to the current thread (usually the active resource object), ornull if nonethe key to check (usually the resource factory) key)Retrieve a resource for the given key that is bound to the current thread. |
| public static Map | Returns: a Map with resource keys (usually the resource factory) and resource values (usually the active resource object), or an empty Map if there are currently no resources boundReturn all resources that are bound to the current thread. |
| public static List | Returns: unmodifiable List of TransactionSynchronization instancesReturn an unmodifiable snapshot list of all registered synchronizations for the current thread. |
| public static boolean | Returns: if there is a value bound to the current threadthe key to check (usually the resource factory) key)Check if there is a resource for the given key bound to the current thread. |
| public static void | |
| public static boolean | |
| public static boolean | |
| public static boolean | |
| public static void | registerSynchronization(TransactionSynchronization
the synchronization object to register synchronization)Register a new transaction synchronization for the current thread. |
| public static void | setActualTransactionActive(boolean
true to mark the current thread as being associated
with an actual transaction; false to reset that markerExpose whether there currently is an actual transaction active. |
| public static void | setCurrentTransactionIsolationLevel(Integer
the isolation level to expose, according to the
JDBC Connection constants (equivalent to the corresponding Spring
TransactionDefinition constants), or isolationLevel)null to reset itExpose an isolation level for the current transaction. |
| public static void | setCurrentTransactionName(String
the name of the transaction, or name)null to reset itExpose the name of the current transaction, if any. |
| public static void | setCurrentTransactionReadOnly(boolean
true to mark the current transaction
as read-only; false to reset such a read-only markerExpose a read-only flag for the current transaction. |
| public static Object | Returns: the previously bound value (usually the active resource object)the key to unbind (usually the resource factory) key)Unbind a resource for the given key from the current thread. |
| public static Object | Returns: the previously bound value, ornull if none boundthe key to unbind (usually the resource factory) key)Unbind a resource for the given key from the current thread. |
| actualTransactionActive | back to summary |
|---|---|
| private static final ThreadLocal<Boolean> actualTransactionActive | |
| currentTransactionIsolationLevel | back to summary |
|---|---|
| private static final ThreadLocal<Integer> currentTransactionIsolationLevel | |
| currentTransactionName | back to summary |
|---|---|
| private static final ThreadLocal<String> currentTransactionName | |
| currentTransactionReadOnly | back to summary |
|---|---|
| private static final ThreadLocal<Boolean> currentTransactionReadOnly | |
| resources | back to summary |
|---|---|
| private static final ThreadLocal<Map<Object, Object>> resources | |
| synchronizations | back to summary |
|---|---|
| private static final ThreadLocal<Set<TransactionSynchronization>> synchronizations | |
| TransactionSynchronizationManager | back to summary |
|---|---|
| public TransactionSynchronizationManager() | |
| bindResource | back to summary |
|---|---|
| public static void bindResource(Object key, Object value) throws IllegalStateException Bind the given resource for the given key to the current thread.
| |
| clear | back to summary |
|---|---|
| public static void clear() Clear the entire transaction synchronization state for the current thread: registered synchronizations as well as the various transaction characteristics. | |
| clearSynchronization | back to summary |
|---|---|
| public static void clearSynchronization() throws IllegalStateException Deactivate transaction synchronization for the current thread. Called by the transaction manager on transaction cleanup.
| |
| doGetResource | back to summary |
|---|---|
| private static Object doGetResource(Object actualKey) Actually check the value of the resource that is bound for the given key.
| |
| doUnbindResource | back to summary |
|---|---|
| private static Object doUnbindResource(Object actualKey) Actually remove the value of the resource that is bound for the given key.
| |
| getCurrentTransactionName | back to summary |
|---|---|
| public static String getCurrentTransactionName() Return the name of the current transaction, or
| |
| getResource | back to summary |
|---|---|
| public static Object getResource(Object key) Retrieve a resource for the given key that is bound to the current thread.
| |
| getResourceMap | back to summary |
|---|---|
| public static Map Return all resources that are bound to the current thread. Mainly for debugging purposes. Resource managers should always invoke
| |
| getSynchronizations | back to summary |
|---|---|
| public static List Return an unmodifiable snapshot list of all registered synchronizations for the current thread.
| |
| hasResource | back to summary |
|---|---|
| public static boolean hasResource(Object key) Check if there is a resource for the given key bound to the current thread.
| |
| initSynchronization | back to summary |
|---|---|
| public static void initSynchronization() throws IllegalStateException Activate transaction synchronization for the current thread. Called by a transaction manager on transaction begin.
| |
| isActualTransactionActive | back to summary |
|---|---|
| public static boolean isActualTransactionActive() Return whether there currently is an actual transaction active. This indicates whether the current thread is associated with an actual transaction rather than just with active transaction synchronization. To be called by resource management code that wants to differentiate between active transaction synchronization (with or without a backing resource transaction; also on PROPAGATION_SUPPORTS) and an actual transaction being active (with a backing resource transaction; on PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW, etc).
| |
| isCurrentTransactionReadOnly | back to summary |
|---|---|
| public static boolean isCurrentTransactionReadOnly() Return whether the current transaction is marked as read-only. To be called by resource management code when preparing a newly created resource (for example, a Hibernate Session). Note that transaction synchronizations receive the read-only flag
as argument for the | |
| isSynchronizationActive | back to summary |
|---|---|
| public static boolean isSynchronizationActive() Return if transaction synchronization is active for the current thread. Can be called before register to avoid unnecessary instance creation.
| |
| registerSynchronization | back to summary |
|---|---|
| public static void registerSynchronization(TransactionSynchronization synchronization) throws IllegalStateException Register a new transaction synchronization for the current thread. Typically called by resource management code. Note that synchronizations can implement the
| |
| setActualTransactionActive | back to summary |
|---|---|
| public static void setActualTransactionActive(boolean active) Expose whether there currently is an actual transaction active. Called by the transaction manager on transaction begin and on cleanup.
| |
| setCurrentTransactionName | back to summary |
|---|---|
| public static void setCurrentTransactionName(String name) Expose the name of the current transaction, if any. Called by the transaction manager on transaction begin and on cleanup.
| |
| setCurrentTransactionReadOnly | back to summary |
|---|---|
| public static void setCurrentTransactionReadOnly(boolean readOnly) Expose a read-only flag for the current transaction. Called by the transaction manager on transaction begin and on cleanup.
| |
| unbindResource | back to summary |
|---|---|
| public static Object unbindResource(Object key) throws IllegalStateException Unbind a resource for the given key from the current thread.
| |
| unbindResourceIfPossible | back to summary |
|---|---|
| public static Object unbindResourceIfPossible(Object key) Unbind a resource for the given key from the current thread. | |