Class PersistentObjectTransactionManager<T>
- Type Parameters:
T
- type of the root object
- All Implemented Interfaces:
Serializable
,Aware
,BeanNameAware
,InitializingBean
,ConfigurableTransactionManager
,PlatformTransactionManager
,ResourceTransactionManager
,TransactionManager
PersistentObject
implementation of Spring's
PlatformTransactionManager
interface,
allowing methods annotated with Spring's @Transactional
annotation to perform transactions on PersistentObject
s.
During a transaction, the transaction's root object is available via the method getRoot()
, and may be replaced entirely
via setRoot()
. When the transaction completes, the transaction's root object will be automatically
written back to the PersistentObject
via PersistentObject.setRoot()
(unless the transaction was read-only). During commit, the PersistentObject
version number is verified, and
if another update has occurred since the transaction was opened, a PersistentObjectVersionException
is thrown
(consider using @RetryTransaction
for automatic retry in this case).
Transactions are associated with threads; the PersistentObjectTransactionManager
associated with the current thread's
transaction is available via getCurrent()
(or getCurrent(String)
if there is more than one).
The persistentObject
property is required. The readOnlySharedRoot
property is optional and configures whether,
during read-only transactions only, getRoot()
returns the shared root object.
In this mode, root object graph copies are avoided entirely for read-only transactions, but the application code must be
careful to not modify the object graph returned by getRoot()
at any time, either during or after the transaction.
This class also provides an XAResource
for use with JTA/XA tranasactions. Once elisted in a JTA transaction,
the usage described above using getRoot()
and setRoot()
is identical.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager
AbstractPlatformTransactionManager.SuspendedResourcesHolder
-
Field Summary
Modifier and TypeFieldDescriptionprotected PersistentObject<T>
The configuredPersistentObject
from which transactions are created.Fields inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager
logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
protected void
doBegin
(Object txObj, TransactionDefinition txDef) protected void
doCleanupAfterCompletion
(Object txObj) protected void
doCommit
(DefaultTransactionStatus status) protected Object
protected void
Resume a previously suspended transaction.protected void
doRollback
(DefaultTransactionStatus status) protected void
protected Object
Suspend the current transaction.Get this instance's assigned bean name.static <T> PersistentObjectTransactionManager<T>
Get the (unique) instance associated with an open transaction in the current thread.static <T> PersistentObjectTransactionManager<T>
getCurrent
(String beanName) Get the instance associated with an open transaction in the current thread and having the given bean name.Get thePersistentObject
that this instance will operate on.getRoot()
Get the root object graph for use in the transaction associated with the current thread.long
Get the snapshot version of the transaction associated with the current thread.Get theXAResource
corresponding to this instance.protected boolean
isExistingTransaction
(Object txObj) boolean
Determine whether the transaction associated with the current thread is a read-only transaction.boolean
Get whether, during read-only transactions,getRoot()
returns a new copy of thePersistentObject
object graph or the shared root.protected void
void
setBeanName
(String beanName) void
setPersistentObject
(PersistentObject<T> persistentObject) Configure thePersistentObject
that this instance will operate on.void
setReadOnlySharedRoot
(boolean readOnlySharedRoot) Configure whether, during read-only transactions,getRoot()
returns a new copy of thePersistentObject
object graph or the shared root.void
Change the root object graph to be committed in the transaction associated with the current thread.Methods inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager
commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionExecutionListeners, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, prepareSynchronization, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionExecutionListeners, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.transaction.ConfigurableTransactionManager
addListener
Methods inherited from interface org.springframework.transaction.PlatformTransactionManager
commit, getTransaction, rollback
-
Field Details
-
persistentObject
The configuredPersistentObject
from which transactions are created.
-
-
Constructor Details
-
PersistentObjectTransactionManager
public PersistentObjectTransactionManager()
-
-
Method Details
-
getBeanName
Get this instance's assigned bean name.- Returns:
- bean name
-
setBeanName
- Specified by:
setBeanName
in interfaceBeanNameAware
-
afterPropertiesSet
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
- Throws:
Exception
-
getPersistentObject
Get thePersistentObject
that this instance will operate on.- Returns:
- the associated persistent object database
-
setPersistentObject
Configure thePersistentObject
that this instance will operate on.Required property.
- Parameters:
persistentObject
- associated persistent object database
-
getXAResource
Get theXAResource
corresponding to this instance.- Returns:
- associated
XAResource
-
getCurrent
Get the instance associated with an open transaction in the current thread and having the given bean name.- Type Parameters:
T
- root object type- Parameters:
beanName
- bean name assigned to the desired instance- Returns:
- current thread's transaction manager with the given name
- Throws:
IllegalArgumentException
- ifname
is nullIllegalStateException
- if the current thread is not running within a transaction managed by aPersistentObjectTransactionManager
assigned the given name
-
getCurrent
Get the (unique) instance associated with an open transaction in the current thread.This is a convenience method for the common case where there is only one instance associated with the current thread.
- Type Parameters:
T
- root object type- Returns:
- current thread's transaction manager
- Throws:
IllegalStateException
- if the current thread is not running within aPersistentObjectTransactionManager
transactionIllegalStateException
- there is more than onePersistentObjectTransactionManager
transaction open in the current thread
-
isReadOnly
public boolean isReadOnly()Determine whether the transaction associated with the current thread is a read-only transaction.- Returns:
- true if the transaction associated with the current thread is a read-only transaction
- Throws:
IllegalStateException
- if there is no transaction associated with the current thread
-
getSnapshotVersion
public long getSnapshotVersion()Get the snapshot version of the transaction associated with the current thread.- Returns:
- snapshot version of the transaction associated with the current thread.
- Throws:
IllegalStateException
- if there is no transaction associated with the current thread
-
getRoot
Get the root object graph for use in the transaction associated with the current thread.- Returns:
- root object
- Throws:
IllegalStateException
- if there is no open transaction
-
setRoot
Change the root object graph to be committed in the transaction associated with the current thread. Subsequent calls togetRoot
will return the new object.This method may be invoked during read-only transactions (as before, the root will not actually be committed).
- Parameters:
root
- new root object- Throws:
IllegalStateException
- if there is no transaction associated with the current threadIllegalArgumentException
- ifroot
is null
-
getResourceFactory
- Specified by:
getResourceFactory
in interfaceResourceTransactionManager
-
doGetTransaction
- Specified by:
doGetTransaction
in classAbstractPlatformTransactionManager
-
isExistingTransaction
- Overrides:
isExistingTransaction
in classAbstractPlatformTransactionManager
-
doBegin
- Specified by:
doBegin
in classAbstractPlatformTransactionManager
-
doSuspend
Suspend the current transaction.- Overrides:
doSuspend
in classAbstractPlatformTransactionManager
-
doResume
Resume a previously suspended transaction.- Overrides:
doResume
in classAbstractPlatformTransactionManager
-
prepareForCommit
- Overrides:
prepareForCommit
in classAbstractPlatformTransactionManager
-
doCommit
- Specified by:
doCommit
in classAbstractPlatformTransactionManager
-
doRollback
- Specified by:
doRollback
in classAbstractPlatformTransactionManager
-
doSetRollbackOnly
- Overrides:
doSetRollbackOnly
in classAbstractPlatformTransactionManager
-
doCleanupAfterCompletion
- Overrides:
doCleanupAfterCompletion
in classAbstractPlatformTransactionManager
-