public class JtaTransactionManager extends AbstractPlatformTransactionManager implements TransactionFactory, InitializingBean, java.io.Serializable
PlatformTransactionManager
implementation
for JTA, delegating to a backend JTA provider. This is typically used to delegate
to a Java EE server's transaction coordinator, but may also be configured with a
local JTA provider which is embedded within the application.
This transaction manager is appropriate for handling distributed transactions, i.e. transactions that span multiple resources, and for controlling transactions on application server resources (e.g. JDBC DataSources available in JNDI) in general. For a single JDBC DataSource, DataSourceTransactionManager is perfectly sufficient, and for accessing a single resource with Hibernate (including transactional cache), HibernateTransactionManager is appropriate, for example.
For typical JTA transactions (REQUIRED, SUPPORTS, MANDATORY, NEVER), a plain JtaTransactionManager definition is all you need, portable across all Java EE servers. This corresponds to the functionality of the JTA UserTransaction, for which Java EE specifies a standard JNDI name ("java:comp/UserTransaction"). There is no need to configure a server-specific TransactionManager lookup for this kind of JTA usage.
Transaction suspension (REQUIRES_NEW, NOT_SUPPORTED) is just available with a JTA TransactionManager being registered. Common TransactionManager locations are autodetected by JtaTransactionManager, provided that the "autodetectTransactionManager" flag is set to "true" (which it is by default).
Note: Support for the JTA TransactionManager interface is not required by Java EE.
Almost all Java EE servers expose it, but do so as extension to EE. There might be some
issues with compatibility, despite the TransactionManager interface being part of JTA.
As a consequence, Spring provides various vendor-specific PlatformTransactionManagers,
which are recommended to be used if appropriate: WebLogicJtaTransactionManager
and WebSphereUowTransactionManager
. For all other Java EE servers, the
standard JtaTransactionManager is sufficient.
This pure JtaTransactionManager class supports timeouts but not per-transaction
isolation levels. Custom subclasses may override the doJtaBegin(org.springframework.transaction.jta.JtaTransactionObject, org.springframework.transaction.TransactionDefinition)
method for
specific JTA extensions in order to provide this functionality; Spring includes a
corresponding WebLogicJtaTransactionManager
class for WebLogic Server. Such
adapters for specific Java EE transaction coordinators may also expose transaction
names for monitoring; with standard JTA, transaction names will simply be ignored.
Consider using Spring's tx:jta-transaction-manager
configuration
element for automatically picking the appropriate JTA platform transaction manager
(automatically detecting WebLogic and WebSphere).
JTA 1.1 adds the TransactionSynchronizationRegistry facility, as public Java EE 5 API in addition to the standard JTA UserTransaction handle. As of Spring 2.5, this JtaTransactionManager autodetects the TransactionSynchronizationRegistry and uses it for registering Spring-managed synchronizations when participating in an existing JTA transaction (e.g. controlled by EJB CMT). If no TransactionSynchronizationRegistry is available, then such synchronizations will be registered via the (non-EE) JTA TransactionManager handle.
This class is serializable. However, active synchronizations do not survive serialization.
javax.transaction.UserTransaction
,
javax.transaction.TransactionManager
,
javax.transaction.TransactionSynchronizationRegistry
,
setUserTransactionName(java.lang.String)
,
setUserTransaction(UserTransaction)
,
setTransactionManagerName(java.lang.String)
,
setTransactionManager(TransactionManager)
,
WebLogicJtaTransactionManager
,
Serialized FormAbstractPlatformTransactionManager.SuspendedResourcesHolder
Modifier and Type | Field and Description |
---|---|
private boolean |
allowCustomIsolationLevels |
private boolean |
autodetectTransactionManager |
private boolean |
autodetectTransactionSynchronizationRegistry |
private boolean |
autodetectUserTransaction |
private boolean |
cacheUserTransaction |
static java.lang.String |
DEFAULT_TRANSACTION_SYNCHRONIZATION_REGISTRY_NAME
Standard Java EE 5 JNDI location for the JTA TransactionSynchronizationRegistry.
|
static java.lang.String |
DEFAULT_USER_TRANSACTION_NAME
Default JNDI location for the JTA UserTransaction.
|
static java.lang.String[] |
FALLBACK_TRANSACTION_MANAGER_NAMES
Fallback JNDI locations for the JTA TransactionManager.
|
private JndiTemplate |
jndiTemplate |
private TransactionManager |
transactionManager |
private java.lang.String |
transactionManagerName |
private TransactionSynchronizationRegistry |
transactionSynchronizationRegistry |
private java.lang.String |
transactionSynchronizationRegistryName |
private UserTransaction |
userTransaction |
private java.lang.String |
userTransactionName |
private boolean |
userTransactionObtainedFromJndi |
logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
Constructor and Description |
---|
JtaTransactionManager()
Create a new JtaTransactionManager instance, to be configured as bean.
|
JtaTransactionManager(TransactionManager transactionManager)
Create a new JtaTransactionManager instance.
|
JtaTransactionManager(UserTransaction userTransaction)
Create a new JtaTransactionManager instance.
|
JtaTransactionManager(UserTransaction userTransaction,
TransactionManager transactionManager)
Create a new JtaTransactionManager instance.
|
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Initialize the UserTransaction as well as the TransactionManager handle.
|
protected void |
applyIsolationLevel(JtaTransactionObject txObject,
int isolationLevel)
Apply the given transaction isolation level.
|
protected void |
applyTimeout(JtaTransactionObject txObject,
int timeout)
Apply the given transaction timeout.
|
protected UserTransaction |
buildUserTransaction(TransactionManager transactionManager)
Build a UserTransaction handle based on the given TransactionManager.
|
protected void |
checkUserTransactionAndTransactionManager()
Check the UserTransaction as well as the TransactionManager handle,
assuming standard JTA requirements.
|
Transaction |
createTransaction(java.lang.String name,
int timeout)
Create an active Transaction object based on the given name and timeout.
|
protected void |
doBegin(java.lang.Object transaction,
TransactionDefinition definition)
Begin a new transaction with semantics according to the given transaction
definition.
|
protected void |
doCleanupAfterCompletion(java.lang.Object transaction)
Cleanup resources after transaction completion.
|
protected void |
doCommit(DefaultTransactionStatus status)
Perform an actual commit of the given transaction.
|
protected JtaTransactionObject |
doGetJtaTransaction(UserTransaction ut)
Get a JTA transaction object for the given current UserTransaction.
|
protected java.lang.Object |
doGetTransaction()
This implementation returns a JtaTransactionObject instance for the
JTA UserTransaction.
|
protected void |
doJtaBegin(JtaTransactionObject txObject,
TransactionDefinition definition)
Perform a JTA begin on the JTA UserTransaction or TransactionManager.
|
protected void |
doJtaResume(JtaTransactionObject txObject,
java.lang.Object suspendedTransaction)
Perform a JTA resume on the JTA TransactionManager.
|
protected java.lang.Object |
doJtaSuspend(JtaTransactionObject txObject)
Perform a JTA suspend on the JTA TransactionManager.
|
protected void |
doRegisterAfterCompletionWithJtaTransaction(JtaTransactionObject txObject,
java.util.List<TransactionSynchronization> synchronizations)
Register a JTA synchronization on the JTA TransactionManager, for calling
afterCompletion on the given Spring TransactionSynchronizations. |
protected void |
doResume(java.lang.Object transaction,
java.lang.Object suspendedResources)
Resume the resources of the current transaction.
|
protected void |
doRollback(DefaultTransactionStatus status)
Perform an actual rollback of the given transaction.
|
protected void |
doSetRollbackOnly(DefaultTransactionStatus status)
Set the given transaction rollback-only.
|
protected java.lang.Object |
doSuspend(java.lang.Object transaction)
Suspend the resources of the current transaction.
|
protected TransactionManager |
findTransactionManager(UserTransaction ut)
Find the JTA TransactionManager through autodetection: checking whether the
UserTransaction object implements the TransactionManager, and checking the
fallback JNDI locations.
|
protected TransactionSynchronizationRegistry |
findTransactionSynchronizationRegistry(UserTransaction ut,
TransactionManager tm)
Find the JTA 1.1 TransactionSynchronizationRegistry through autodetection:
checking whether the UserTransaction object or TransactionManager object
implements it, and checking Java EE 5's standard JNDI location.
|
protected UserTransaction |
findUserTransaction()
Find the JTA UserTransaction through a default JNDI lookup:
"java:comp/UserTransaction".
|
java.util.Properties |
getJndiEnvironment()
Return the JNDI environment to use for JNDI lookups.
|
JndiTemplate |
getJndiTemplate()
Return the JndiTemplate used for JNDI lookups.
|
TransactionManager |
getTransactionManager()
Return the JTA TransactionManager that this transaction manager uses, if any.
|
TransactionSynchronizationRegistry |
getTransactionSynchronizationRegistry()
Return the JTA 1.1 TransactionSynchronizationRegistry that this transaction manager uses, if any.
|
UserTransaction |
getUserTransaction()
Return the JTA UserTransaction that this transaction manager uses.
|
protected void |
initTransactionSynchronizationRegistry()
Initialize the JTA 1.1 TransactionSynchronizationRegistry, if available.
|
protected void |
initUserTransactionAndTransactionManager()
Initialize the UserTransaction as well as the TransactionManager handle.
|
protected boolean |
isExistingTransaction(java.lang.Object transaction)
Check if the given transaction object indicates an existing transaction
(that is, a transaction which has already started).
|
protected TransactionManager |
lookupTransactionManager(java.lang.String transactionManagerName)
Look up the JTA TransactionManager in JNDI via the configured name.
|
protected TransactionSynchronizationRegistry |
lookupTransactionSynchronizationRegistry(java.lang.String registryName)
Look up the JTA 1.1 TransactionSynchronizationRegistry in JNDI via the configured name.
|
protected UserTransaction |
lookupUserTransaction(java.lang.String userTransactionName)
Look up the JTA UserTransaction in JNDI via the configured name.
|
private void |
readObject(java.io.ObjectInputStream ois) |
protected void |
registerAfterCompletionWithExistingTransaction(java.lang.Object transaction,
java.util.List<TransactionSynchronization> synchronizations)
Register the given list of transaction synchronizations with the existing transaction.
|
protected TransactionManager |
retrieveTransactionManager()
Allows subclasses to retrieve the JTA TransactionManager in a vendor-specific manner.
|
protected TransactionSynchronizationRegistry |
retrieveTransactionSynchronizationRegistry()
Allows subclasses to retrieve the JTA 1.1 TransactionSynchronizationRegistry
in a vendor-specific manner.
|
protected UserTransaction |
retrieveUserTransaction()
Allows subclasses to retrieve the JTA UserTransaction in a vendor-specific manner.
|
void |
setAllowCustomIsolationLevels(boolean allowCustomIsolationLevels)
Set whether to allow custom isolation levels to be specified.
|
void |
setAutodetectTransactionManager(boolean autodetectTransactionManager)
Set whether to autodetect a JTA UserTransaction object that implements
the JTA TransactionManager interface too (i.e.
|
void |
setAutodetectTransactionSynchronizationRegistry(boolean autodetectTransactionSynchronizationRegistry)
Set whether to autodetect a JTA 1.1 TransactionSynchronizationRegistry object
at its default JDNI location ("java:comp/TransactionSynchronizationRegistry")
if the UserTransaction has also been obtained from JNDI, and also whether
to fall back to checking whether the JTA UserTransaction/TransactionManager
object implements the JTA TransactionSynchronizationRegistry interface too.
|
void |
setAutodetectUserTransaction(boolean autodetectUserTransaction)
Set whether to autodetect the JTA UserTransaction at its default
JNDI location "java:comp/UserTransaction", as specified by Java EE.
|
void |
setCacheUserTransaction(boolean cacheUserTransaction)
Set whether to cache the JTA UserTransaction object fetched from JNDI.
|
void |
setJndiEnvironment(java.util.Properties jndiEnvironment)
Set the JNDI environment to use for JNDI lookups.
|
void |
setJndiTemplate(JndiTemplate jndiTemplate)
Set the JndiTemplate to use for JNDI lookups.
|
void |
setTransactionManager(TransactionManager transactionManager)
Set the JTA TransactionManager to use as direct reference.
|
void |
setTransactionManagerName(java.lang.String transactionManagerName)
Set the JNDI name of the JTA TransactionManager.
|
void |
setTransactionSynchronizationRegistry(TransactionSynchronizationRegistry transactionSynchronizationRegistry)
Set the JTA 1.1 TransactionSynchronizationRegistry to use as direct reference.
|
void |
setTransactionSynchronizationRegistryName(java.lang.String transactionSynchronizationRegistryName)
Set the JNDI name of the JTA 1.1 TransactionSynchronizationRegistry.
|
void |
setUserTransaction(UserTransaction userTransaction)
Set the JTA UserTransaction to use as direct reference.
|
void |
setUserTransactionName(java.lang.String userTransactionName)
Set the JNDI name of the JTA UserTransaction.
|
protected boolean |
shouldCommitOnGlobalRollbackOnly()
This implementation returns "true": a JTA commit will properly handle
transactions that have been marked rollback-only at a global level.
|
boolean |
supportsResourceAdapterManagedTransactions()
Determine whether the underlying transaction manager supports XA transactions
managed by a resource adapter (i.e.
|
protected boolean |
useSavepointForNestedTransaction()
This implementation returns false to cause a further invocation
of doBegin despite an already existing transaction.
|
commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, prepareSynchronization, prepareTransactionStatus, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, suspend, triggerBeforeCommit, triggerBeforeCompletion
public static final java.lang.String DEFAULT_USER_TRANSACTION_NAME
public static final java.lang.String[] FALLBACK_TRANSACTION_MANAGER_NAMES
public static final java.lang.String DEFAULT_TRANSACTION_SYNCHRONIZATION_REGISTRY_NAME
private transient JndiTemplate jndiTemplate
private transient UserTransaction userTransaction
private java.lang.String userTransactionName
private boolean autodetectUserTransaction
private boolean cacheUserTransaction
private boolean userTransactionObtainedFromJndi
private transient TransactionManager transactionManager
private java.lang.String transactionManagerName
private boolean autodetectTransactionManager
private transient TransactionSynchronizationRegistry transactionSynchronizationRegistry
private java.lang.String transactionSynchronizationRegistryName
private boolean autodetectTransactionSynchronizationRegistry
private boolean allowCustomIsolationLevels
public JtaTransactionManager()
afterPropertiesSet
to activate the configuration.public JtaTransactionManager(UserTransaction userTransaction)
userTransaction
- the JTA UserTransaction to use as direct referencepublic JtaTransactionManager(UserTransaction userTransaction, TransactionManager transactionManager)
userTransaction
- the JTA UserTransaction to use as direct referencetransactionManager
- the JTA TransactionManager to use as direct referencepublic JtaTransactionManager(TransactionManager transactionManager)
transactionManager
- the JTA TransactionManager to use as direct referencepublic void setJndiTemplate(JndiTemplate jndiTemplate)
public JndiTemplate getJndiTemplate()
public void setJndiEnvironment(java.util.Properties jndiEnvironment)
public java.util.Properties getJndiEnvironment()
public void setUserTransaction(UserTransaction userTransaction)
Typically just used for local JTA setups; in a Java EE environment, the UserTransaction will always be fetched from JNDI.
public UserTransaction getUserTransaction()
public void setUserTransactionName(java.lang.String userTransactionName)
Note that the UserTransaction will be autodetected at the Java EE default location "java:comp/UserTransaction" if not specified explicitly.
public void setAutodetectUserTransaction(boolean autodetectUserTransaction)
Default is "true", autodetecting the UserTransaction unless it has been specified explicitly. Turn this flag off to allow for JtaTransactionManager operating against the TransactionManager only, despite a default UserTransaction being available.
DEFAULT_USER_TRANSACTION_NAME
public void setCacheUserTransaction(boolean cacheUserTransaction)
Default is "true": UserTransaction lookup will only happen at startup, reusing the same UserTransaction handle for all transactions of all threads. This is the most efficient choice for all application servers that provide a shared UserTransaction object (the typical case).
Turn this flag off to enforce a fresh lookup of the UserTransaction for every transaction. This is only necessary for application servers that return a new UserTransaction for every transaction, keeping state tied to the UserTransaction object itself rather than the current thread.
setUserTransactionName(java.lang.String)
public void setTransactionManager(TransactionManager transactionManager)
A TransactionManager is necessary for suspending and resuming transactions, as this not supported by the UserTransaction interface.
Note that the TransactionManager will be autodetected if the JTA UserTransaction object implements the JTA TransactionManager interface too, as well as autodetected at various well-known fallback JNDI locations.
public TransactionManager getTransactionManager()
public void setTransactionManagerName(java.lang.String transactionManagerName)
A TransactionManager is necessary for suspending and resuming transactions, as this not supported by the UserTransaction interface.
Note that the TransactionManager will be autodetected if the JTA UserTransaction object implements the JTA TransactionManager interface too, as well as autodetected at various well-known fallback JNDI locations.
public void setAutodetectTransactionManager(boolean autodetectTransactionManager)
Default is "true", autodetecting the TransactionManager unless it has been specified explicitly. Can be turned off to deliberately ignore an available TransactionManager, for example when there are known issues with suspend/resume and any attempt to use REQUIRES_NEW or NOT_SUPPORTED should fail fast.
FALLBACK_TRANSACTION_MANAGER_NAMES
public void setTransactionSynchronizationRegistry(TransactionSynchronizationRegistry transactionSynchronizationRegistry)
A TransactionSynchronizationRegistry allows for interposed registration of transaction synchronizations, as an alternative to the regular registration methods on the JTA TransactionManager API. Also, it is an official part of the Java EE 5 platform, in contrast to the JTA TransactionManager itself.
Note that the TransactionSynchronizationRegistry will be autodetected in JNDI and also from the UserTransaction/TransactionManager object if implemented there as well.
public TransactionSynchronizationRegistry getTransactionSynchronizationRegistry()
public void setTransactionSynchronizationRegistryName(java.lang.String transactionSynchronizationRegistryName)
Note that the TransactionSynchronizationRegistry will be autodetected at the Java EE 5 default location "java:comp/TransactionSynchronizationRegistry" if not specified explicitly.
public void setAutodetectTransactionSynchronizationRegistry(boolean autodetectTransactionSynchronizationRegistry)
Default is "true", autodetecting the TransactionSynchronizationRegistry unless it has been specified explicitly. Can be turned off to delegate synchronization registration to the regular JTA TransactionManager API.
public void setAllowCustomIsolationLevels(boolean allowCustomIsolationLevels)
Default is "false", throwing an exception if a non-default isolation level is specified for a transaction. Turn this flag on if affected resource adapters check the thread-bound transaction context and apply the specified isolation levels individually (e.g. through an IsolationLevelDataSourceAdapter).
public void afterPropertiesSet() throws TransactionSystemException
afterPropertiesSet
in interface InitializingBean
TransactionSystemException
initUserTransactionAndTransactionManager()
protected void initUserTransactionAndTransactionManager() throws TransactionSystemException
TransactionSystemException
- if initialization failedprotected void checkUserTransactionAndTransactionManager() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if no sufficient handles are availableprotected void initTransactionSynchronizationRegistry()
To be called after initUserTransactionAndTransactionManager()
,
since it may check the UserTransaction and TransactionManager handles.
TransactionSystemException
- if initialization failedprotected UserTransaction buildUserTransaction(TransactionManager transactionManager)
transactionManager
- the TransactionManagerprotected UserTransaction lookupUserTransaction(java.lang.String userTransactionName) throws TransactionSystemException
Called by afterPropertiesSet
if no direct UserTransaction reference was set.
Can be overridden in subclasses to provide a different UserTransaction object.
userTransactionName
- the JNDI name of the UserTransactionTransactionSystemException
- if the JNDI lookup failedsetJndiTemplate(org.springframework.jndi.JndiTemplate)
,
setUserTransactionName(java.lang.String)
protected TransactionManager lookupTransactionManager(java.lang.String transactionManagerName) throws TransactionSystemException
Called by afterPropertiesSet
if no direct TransactionManager reference was set.
Can be overridden in subclasses to provide a different TransactionManager object.
transactionManagerName
- the JNDI name of the TransactionManagerTransactionSystemException
- if the JNDI lookup failedsetJndiTemplate(org.springframework.jndi.JndiTemplate)
,
setTransactionManagerName(java.lang.String)
protected TransactionSynchronizationRegistry lookupTransactionSynchronizationRegistry(java.lang.String registryName) throws TransactionSystemException
Can be overridden in subclasses to provide a different TransactionManager object.
registryName
- the JNDI name of the
TransactionSynchronizationRegistryTransactionSystemException
- if the JNDI lookup failedsetJndiTemplate(org.springframework.jndi.JndiTemplate)
,
setTransactionSynchronizationRegistryName(java.lang.String)
protected UserTransaction retrieveUserTransaction() throws TransactionSystemException
The default implementation simply returns null
.
null
if none foundTransactionSystemException
- in case of errorssetUserTransaction(UserTransaction)
,
setUserTransactionName(java.lang.String)
protected TransactionManager retrieveTransactionManager() throws TransactionSystemException
The default implementation simply returns null
.
null
if none foundTransactionSystemException
- in case of errorssetTransactionManager(TransactionManager)
,
setTransactionManagerName(java.lang.String)
protected TransactionSynchronizationRegistry retrieveTransactionSynchronizationRegistry() throws TransactionSystemException
The default implementation simply returns null
.
null
if none foundTransactionSystemException
- in case of errorsprotected UserTransaction findUserTransaction()
null
if not foundDEFAULT_USER_TRANSACTION_NAME
protected TransactionManager findTransactionManager(UserTransaction ut)
ut
- the JTA UserTransaction objectnull
if not foundFALLBACK_TRANSACTION_MANAGER_NAMES
protected TransactionSynchronizationRegistry findTransactionSynchronizationRegistry(UserTransaction ut, TransactionManager tm) throws TransactionSystemException
The default implementation simply returns null
.
ut
- the JTA UserTransaction objecttm
- the JTA TransactionManager objectnull
if none foundTransactionSystemException
- in case of errorsprotected java.lang.Object doGetTransaction()
The UserTransaction object will either be looked up freshly for the current transaction, or the cached one looked up at startup will be used. The latter is the default: Most application servers use a shared singleton UserTransaction that can be cached. Turn off the "cacheUserTransaction" flag to enforce a fresh lookup for every transaction.
doGetTransaction
in class AbstractPlatformTransactionManager
setCacheUserTransaction(boolean)
protected JtaTransactionObject doGetJtaTransaction(UserTransaction ut)
Subclasses can override this to provide a JtaTransactionObject subclass, for example holding some additional JTA handle needed.
ut
- the UserTransaction handle to use for the current transactionprotected boolean isExistingTransaction(java.lang.Object transaction)
AbstractPlatformTransactionManager
The result will be evaluated according to the specified propagation behavior for the new transaction. An existing transaction might get suspended (in case of PROPAGATION_REQUIRES_NEW), or the new transaction might participate in the existing one (in case of PROPAGATION_REQUIRED).
The default implementation returns false
, assuming that
participating in existing transactions is generally not supported.
Subclasses are of course encouraged to provide such support.
isExistingTransaction
in class AbstractPlatformTransactionManager
transaction
- transaction object returned by doGetTransactionAbstractPlatformTransactionManager.doGetTransaction()
protected boolean useSavepointForNestedTransaction()
JTA implementations might support nested transactions via further
UserTransaction.begin()
invocations, but never support savepoints.
useSavepointForNestedTransaction
in class AbstractPlatformTransactionManager
doBegin(java.lang.Object, org.springframework.transaction.TransactionDefinition)
,
javax.transaction.UserTransaction#begin()
protected void doBegin(java.lang.Object transaction, TransactionDefinition definition)
AbstractPlatformTransactionManager
This method gets called when the transaction manager has decided to actually start a new transaction. Either there wasn't any transaction before, or the previous transaction has been suspended.
A special scenario is a nested transaction without savepoint: If
useSavepointForNestedTransaction()
returns "false", this method
will be called to start a nested transaction when necessary. In such a context,
there will be an active transaction: The implementation of this method has
to detect this and start an appropriate nested transaction.
doBegin
in class AbstractPlatformTransactionManager
transaction
- transaction object returned by doGetTransaction
definition
- TransactionDefinition instance, describing propagation
behavior, isolation level, read-only flag, timeout, and transaction nameprotected void doJtaBegin(JtaTransactionObject txObject, TransactionDefinition definition) throws NotSupportedException, SystemException
This implementation only supports standard JTA functionality: that is, no per-transaction isolation levels and no transaction names. Can be overridden in subclasses, for specific JTA implementations.
Calls applyIsolationLevel
and applyTimeout
before invoking the UserTransaction's begin
method.
txObject
- the JtaTransactionObject containing the UserTransactiondefinition
- TransactionDefinition instance, describing propagation
behavior, isolation level, read-only flag, timeout, and transaction nameNotSupportedException
- if thrown by JTA methodsSystemException
- if thrown by JTA methodsgetUserTransaction()
,
getTransactionManager()
,
applyIsolationLevel(org.springframework.transaction.jta.JtaTransactionObject, int)
,
applyTimeout(org.springframework.transaction.jta.JtaTransactionObject, int)
,
JtaTransactionObject.getUserTransaction()
,
javax.transaction.UserTransaction#setTransactionTimeout
,
javax.transaction.UserTransaction#begin
protected void applyIsolationLevel(JtaTransactionObject txObject, int isolationLevel) throws InvalidIsolationLevelException, SystemException
To be overridden in subclasses for specific JTA implementations,
as alternative to overriding the full doJtaBegin(org.springframework.transaction.jta.JtaTransactionObject, org.springframework.transaction.TransactionDefinition)
method.
txObject
- the JtaTransactionObject containing the UserTransactionisolationLevel
- isolation level taken from transaction definitionInvalidIsolationLevelException
- if the given isolation level
cannot be appliedSystemException
- if thrown by the JTA implementationdoJtaBegin(org.springframework.transaction.jta.JtaTransactionObject, org.springframework.transaction.TransactionDefinition)
,
JtaTransactionObject.getUserTransaction()
,
getTransactionManager()
protected void applyTimeout(JtaTransactionObject txObject, int timeout) throws SystemException
UserTransaction.setTransactionTimeout
for a non-default timeout value.txObject
- the JtaTransactionObject containing the UserTransactiontimeout
- timeout value taken from transaction definitionSystemException
- if thrown by the JTA implementationdoJtaBegin(org.springframework.transaction.jta.JtaTransactionObject, org.springframework.transaction.TransactionDefinition)
,
JtaTransactionObject.getUserTransaction()
,
javax.transaction.UserTransaction#setTransactionTimeout(int)
protected java.lang.Object doSuspend(java.lang.Object transaction)
AbstractPlatformTransactionManager
The default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.
doSuspend
in class AbstractPlatformTransactionManager
transaction
- transaction object returned by doGetTransaction
AbstractPlatformTransactionManager.doResume(java.lang.Object, java.lang.Object)
protected java.lang.Object doJtaSuspend(JtaTransactionObject txObject) throws SystemException
Can be overridden in subclasses, for specific JTA implementations.
txObject
- the JtaTransactionObject containing the UserTransactionSystemException
- if thrown by JTA methodsgetTransactionManager()
,
javax.transaction.TransactionManager#suspend()
protected void doResume(java.lang.Object transaction, java.lang.Object suspendedResources)
AbstractPlatformTransactionManager
The default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.
doResume
in class AbstractPlatformTransactionManager
transaction
- transaction object returned by doGetTransaction
suspendedResources
- the object that holds suspended resources,
as returned by doSuspendAbstractPlatformTransactionManager.doSuspend(java.lang.Object)
protected void doJtaResume(JtaTransactionObject txObject, java.lang.Object suspendedTransaction) throws javax.transaction.InvalidTransactionException, SystemException
Can be overridden in subclasses, for specific JTA implementations.
txObject
- the JtaTransactionObject containing the UserTransactionsuspendedTransaction
- the suspended JTA Transaction objectjavax.transaction.InvalidTransactionException
- if thrown by JTA methodsSystemException
- if thrown by JTA methodsgetTransactionManager()
,
javax.transaction.TransactionManager#resume(javax.transaction.Transaction)
protected boolean shouldCommitOnGlobalRollbackOnly()
shouldCommitOnGlobalRollbackOnly
in class AbstractPlatformTransactionManager
AbstractPlatformTransactionManager.doCommit(org.springframework.transaction.support.DefaultTransactionStatus)
,
DefaultTransactionStatus.isGlobalRollbackOnly()
,
AbstractTransactionStatus.isLocalRollbackOnly()
,
TransactionStatus.setRollbackOnly()
,
UnexpectedRollbackException
,
javax.transaction.UserTransaction#commit()
,
javax.transaction.RollbackException
protected void doCommit(DefaultTransactionStatus status)
AbstractPlatformTransactionManager
An implementation does not need to check the "new transaction" flag or the rollback-only flag; this will already have been handled before. Usually, a straight commit will be performed on the transaction object contained in the passed-in status.
doCommit
in class AbstractPlatformTransactionManager
status
- the status representation of the transactionDefaultTransactionStatus.getTransaction()
protected void doRollback(DefaultTransactionStatus status)
AbstractPlatformTransactionManager
An implementation does not need to check the "new transaction" flag; this will already have been handled before. Usually, a straight rollback will be performed on the transaction object contained in the passed-in status.
doRollback
in class AbstractPlatformTransactionManager
status
- the status representation of the transactionDefaultTransactionStatus.getTransaction()
protected void doSetRollbackOnly(DefaultTransactionStatus status)
AbstractPlatformTransactionManager
The default implementation throws an IllegalTransactionStateException, assuming that participating in existing transactions is generally not supported. Subclasses are of course encouraged to provide such support.
doSetRollbackOnly
in class AbstractPlatformTransactionManager
status
- the status representation of the transactionprotected void registerAfterCompletionWithExistingTransaction(java.lang.Object transaction, java.util.List<TransactionSynchronization> synchronizations)
AbstractPlatformTransactionManager
Invoked when the control of the Spring transaction manager and thus all Spring transaction synchronizations end, without the transaction being completed yet. This is for example the case when participating in an existing JTA or EJB CMT transaction.
The default implementation simply invokes the afterCompletion
methods
immediately, passing in "STATUS_UNKNOWN". This is the best we can do if there's no
chance to determine the actual outcome of the outer transaction.
registerAfterCompletionWithExistingTransaction
in class AbstractPlatformTransactionManager
transaction
- transaction object returned by doGetTransaction
synchronizations
- List of TransactionSynchronization objectsAbstractPlatformTransactionManager.invokeAfterCompletion(java.util.List, int)
,
TransactionSynchronization.afterCompletion(int)
,
TransactionSynchronization.STATUS_UNKNOWN
protected void doRegisterAfterCompletionWithJtaTransaction(JtaTransactionObject txObject, java.util.List<TransactionSynchronization> synchronizations) throws RollbackException, SystemException
afterCompletion
on the given Spring TransactionSynchronizations.
The default implementation registers the synchronizations on the JTA 1.1 TransactionSynchronizationRegistry, if available, or on the JTA TransactionManager's current Transaction - again, if available. If none of the two is available, a warning will be logged.
Can be overridden in subclasses, for specific JTA implementations.
txObject
- the current transaction objectsynchronizations
- List of TransactionSynchronization objectsRollbackException
- if thrown by JTA methodsSystemException
- if thrown by JTA methodsgetTransactionManager()
,
javax.transaction.Transaction#registerSynchronization
,
javax.transaction.TransactionSynchronizationRegistry#registerInterposedSynchronization
protected void doCleanupAfterCompletion(java.lang.Object transaction)
AbstractPlatformTransactionManager
Called after doCommit
and doRollback
execution,
on any outcome. The default implementation does nothing.
Should not throw any exceptions but just issue warnings on errors.
doCleanupAfterCompletion
in class AbstractPlatformTransactionManager
transaction
- transaction object returned by doGetTransaction
public Transaction createTransaction(java.lang.String name, int timeout) throws NotSupportedException, SystemException
TransactionFactory
createTransaction
in interface TransactionFactory
name
- the transaction name (may be null
)timeout
- the transaction timeout (may be -1 for the default timeout)null
)NotSupportedException
- if the transaction manager does not support
a transaction of the specified typeSystemException
- if the transaction manager failed to create the
transactionpublic boolean supportsResourceAdapterManagedTransactions()
TransactionFactory
Typically false
. Checked by
AbstractMessageEndpointFactory
in order to differentiate between invalid configuration and valid
ResourceAdapter-managed transactions.
supportsResourceAdapterManagedTransactions
in interface TransactionFactory
javax.resource.spi.ResourceAdapter#endpointActivation
,
javax.resource.spi.endpoint.MessageEndpointFactory#isDeliveryTransacted
private void readObject(java.io.ObjectInputStream ois) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException