org.springframework.transaction.support
Class TransactionSynchronizationManager

java.lang.Object
  extended byorg.springframework.transaction.support.TransactionSynchronizationManager

public abstract class TransactionSynchronizationManager
extends java.lang.Object

Internal class that manages resources and transaction synchronizations per thread. 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. It 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 HibernateTransactionManager and JtaTransactionManager.

Resource management code should only register synchronizations when this manager is active, and perform resource cleanup immediately else. If transaction synchronization isn't active, there is either no current transaction, or the transaction manager doesn't support 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 with JTA.

Since:
02.06.2003
Author:
Juergen Hoeller
See Also:
isSynchronizationActive(), registerSynchronization(org.springframework.transaction.support.TransactionSynchronization), TransactionSynchronization, AbstractPlatformTransactionManager, 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(java.lang.Object key, java.lang.Object value)
          Bind the given resource for the given key to the current thread.
static void clearSynchronization()
          Deactivate thread synchronizations for the current thread.
static java.lang.Object getResource(java.lang.Object key)
          Retrieve a resource for the given key that is bound to the current thread.
static java.util.Map getResourceMap()
          Return all resources that are bound to the current thread.
static java.util.List getSynchronizations()
          Return an unmodifiable list of all registered synchronizations for the current thread.
static boolean hasResource(java.lang.Object key)
          Check if there is a resource for the given key bound to the current thread.
static void initSynchronization()
          Activate thread synchronizations for the current thread.
static boolean isSynchronizationActive()
          Return if thread synchronizations are active for the current thread.
static void registerSynchronization(TransactionSynchronization synchronization)
          Register a new transaction synchronization for the current thread.
static java.lang.Object unbindResource(java.lang.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

TransactionSynchronizationManager

public TransactionSynchronizationManager()
Method Detail

getResourceMap

public static java.util.Map getResourceMap()
Return all resources that are bound to the current thread.

Mainly for debugging purposes. Resource managers should always invoke hasResource for a specific resource key that they are interested in.

Returns:
Map with resource keys and resource objects
See Also:
hasResource(java.lang.Object)

hasResource

public static boolean hasResource(java.lang.Object key)
Check if there is a resource for the given key bound to the current thread.

Parameters:
key - key to check
Returns:
if there is a value bound to the current thread

getResource

public static java.lang.Object getResource(java.lang.Object key)
Retrieve a resource for the given key that is bound to the current thread.

Parameters:
key - key to check
Returns:
a value bound to the current thread, or null if none

bindResource

public static void bindResource(java.lang.Object key,
                                java.lang.Object value)
                         throws java.lang.IllegalStateException
Bind the given resource for the given key to the current thread.

Parameters:
key - key to bind the value to
value - value to bind
Throws:
java.lang.IllegalStateException - if there is already a value bound to the thread

unbindResource

public static java.lang.Object unbindResource(java.lang.Object key)
                                       throws java.lang.IllegalStateException
Unbind a resource for the given key from the current thread.

Parameters:
key - key to check
Returns:
the previously bound value
Throws:
java.lang.IllegalStateException - if there is no value bound to the thread

isSynchronizationActive

public static boolean isSynchronizationActive()
Return if thread synchronizations are active for the current thread. Can be called before register to avoid unnecessary instance creation.

See Also:
registerSynchronization(org.springframework.transaction.support.TransactionSynchronization)

initSynchronization

public static void initSynchronization()
                                throws java.lang.IllegalStateException
Activate thread synchronizations for the current thread. Called by transaction manager on transaction begin.

Throws:
java.lang.IllegalStateException - if synchronization is already active

registerSynchronization

public static void registerSynchronization(TransactionSynchronization synchronization)
                                    throws java.lang.IllegalStateException
Register a new transaction synchronization for the current thread. Called by resource management code.

Throws:
java.lang.IllegalStateException - if synchronization is not active

getSynchronizations

public static java.util.List getSynchronizations()
                                          throws java.lang.IllegalStateException
Return an unmodifiable list of all registered synchronizations for the current thread.

Returns:
unmodifiable List of TransactionSynchronization instances
Throws:
java.lang.IllegalStateException - if synchronization is not active
See Also:
TransactionSynchronization

clearSynchronization

public static void clearSynchronization()
                                 throws java.lang.IllegalStateException
Deactivate thread synchronizations for the current thread. Called by transaction manager on transaction cleanup.

Throws:
java.lang.IllegalStateException - if synchronization is not active


Copyright (C) 2003-2004 The Spring Framework Project.