|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.transaction.support.TransactionSynchronizationManager
public abstract class TransactionSynchronizationManager
Central helper that manages resources and transaction synchronizations per thread. To be used by resource management code but not by typical application code.
Supports one resource per key without overwriting, i.e. 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 responsiblity
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, like DataSourceTransactionManager
and JtaTransactionManager.
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 synchronizations.
Synchronization is for example used to always return the same resources like JDBC Connections or Hibernate Sessions within a JTA transaction, for any given DataSource or SessionFactory. In the Hibernate case, the afterCompletion Session close calls allow for proper transactional JVM-level caching even without a custom TransactionManagerLookup in Hibernate configuration.
isSynchronizationActive()
,
registerSynchronization(org.springframework.transaction.support.TransactionSynchronization)
,
TransactionSynchronization
,
AbstractPlatformTransactionManager
,
DataSourceTransactionManager
,
JtaTransactionManager
,
DataSourceUtils.getConnection(javax.sql.DataSource)
,
SessionFactoryUtils.getSession(net.sf.hibernate.SessionFactory, boolean)
,
PersistenceManagerFactoryUtils.getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean)
Constructor Summary | |
---|---|
TransactionSynchronizationManager()
|
Method Summary | |
---|---|
static void |
bindResource(Object key,
Object value)
Bind the given resource for the given key to the current thread. |
static void |
clearSynchronization()
Deactivate transaction synchronization for the current thread. |
static String |
getCurrentTransactionName()
Return the name of the current transaction, or null if none set. |
static Object |
getResource(Object key)
Retrieve a resource for the given key that is bound to the current thread. |
static Map |
getResourceMap()
Return all resources that are bound to the current thread. |
static List |
getSynchronizations()
Return an unmodifiable snapshot list of all registered synchronizations for the current thread. |
static boolean |
hasResource(Object key)
Check if there is a resource for the given key bound to the current thread. |
static void |
initSynchronization()
Activate transaction synchronization for the current thread. |
static boolean |
isActualTransactionActive()
Return whether there currently is an actual transaction active. |
static boolean |
isCurrentTransactionReadOnly()
Return whether the current transaction is marked as read-only. |
static boolean |
isSynchronizationActive()
Return if transaction synchronization is active for the current thread. |
static void |
registerSynchronization(TransactionSynchronization synchronization)
Register a new transaction synchronization for the current thread. |
static void |
setActualTransactionActive(boolean active)
Expose whether there currently is an actual transaction active. |
static void |
setCurrentTransactionName(String name)
Expose the name of the current transaction, if any. |
static void |
setCurrentTransactionReadOnly(boolean readOnly)
Expose a read-only flag for the current transaction. |
static Object |
unbindResource(Object key)
Unbind a resource for the given key from the current thread. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public TransactionSynchronizationManager()
Method Detail |
---|
public static Map getResourceMap()
Mainly for debugging purposes. Resource managers should always invoke hasResource for a specific resource key that they are interested in.
hasResource(java.lang.Object)
public static boolean hasResource(Object key)
key
- key to check
public static Object getResource(Object key)
key
- key to check
public static void bindResource(Object key, Object value) throws IllegalStateException
key
- key to bind the value tovalue
- value to bind
IllegalStateException
- if there is already a value bound to the threadpublic static Object unbindResource(Object key) throws IllegalStateException
key
- key to check
IllegalStateException
- if there is no value bound to the threadpublic static boolean isSynchronizationActive()
registerSynchronization(org.springframework.transaction.support.TransactionSynchronization)
public static void initSynchronization() throws IllegalStateException
IllegalStateException
- if synchronization is already activepublic static void registerSynchronization(TransactionSynchronization synchronization) throws IllegalStateException
Note that synchronizations can implemented the Ordered interface. They will be executed in an order according to their order value (if any).
IllegalStateException
- if synchronization is not activeOrdered
public static List getSynchronizations() throws IllegalStateException
IllegalStateException
- if synchronization is not activeTransactionSynchronization
public static void clearSynchronization() throws IllegalStateException
IllegalStateException
- if synchronization is not activepublic static void setCurrentTransactionName(String name)
name
- the name of the transaction, or null to reset itpublic static String getCurrentTransactionName()
public static void setCurrentTransactionReadOnly(boolean readOnly)
readOnly
- true to mark the current transaction as read-only;
false to reset such a read-only markerTransactionDefinition.isReadOnly()
public static boolean isCurrentTransactionReadOnly()
Note that transaction synchronizations receive the read-only flag
as argument for the beforeCommit
callback, to be able
to suppress change detection on commit. The present method is meant
to be used for earlier read-only checks, for example to set the
flush mode of a Hibernate Session to FlushMode.NEVER upfront.
TransactionSynchronization.beforeCommit(boolean)
,
Session.flush()
,
Session.setFlushMode(org.hibernate.FlushMode)
,
FlushMode.NEVER
public static void setActualTransactionActive(boolean active)
active
- true to mark the current thread as being associated
with an actual transaction; false to reset that markerpublic static boolean isActualTransactionActive()
To be called by resource management code that wants to discriminate between active transaction synchronization (with or without backing resource transaction; also on PROPAGATION_SUPPORTS) and an actual transaction being active (with backing resource transaction; on PROPAGATION_REQUIRES, PROPAGATION_REQUIRES_NEW, etc).
isSynchronizationActive()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |