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 PersistentObjects.
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
FieldsModifier and TypeFieldDescriptionprotected PersistentObject<T>The configuredPersistentObjectfrom which transactions are created.Fields inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager
logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected voiddoBegin(Object txObj, TransactionDefinition txDef) protected voiddoCleanupAfterCompletion(Object txObj) protected voiddoCommit(DefaultTransactionStatus status) protected Objectprotected voidResume a previously suspended transaction.protected voiddoRollback(DefaultTransactionStatus status) protected voidprotected ObjectSuspend 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 thePersistentObjectthat this instance will operate on.getRoot()Get the root object graph for use in the transaction associated with the current thread.longGet the snapshot version of the transaction associated with the current thread.Get theXAResourcecorresponding to this instance.protected booleanisExistingTransaction(Object txObj) booleanDetermine whether the transaction associated with the current thread is a read-only transaction.booleanGet whether, during read-only transactions,getRoot()returns a new copy of thePersistentObjectobject graph or the shared root.protected voidvoidsetBeanName(String beanName) voidsetPersistentObject(PersistentObject<T> persistentObject) Configure thePersistentObjectthat this instance will operate on.voidsetReadOnlySharedRoot(boolean readOnlySharedRoot) Configure whether, during read-only transactions,getRoot()returns a new copy of thePersistentObjectobject graph or the shared root.voidChange 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, useSavepointForNestedTransactionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.transaction.ConfigurableTransactionManager
addListenerMethods inherited from interface org.springframework.transaction.PlatformTransactionManager
commit, getTransaction, rollback
-
Field Details
-
persistentObject
The configuredPersistentObjectfrom 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:
setBeanNamein interfaceBeanNameAware
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceInitializingBean- Throws:
Exception
-
getPersistentObject
Get thePersistentObjectthat this instance will operate on.- Returns:
- the associated persistent object database
-
setPersistentObject
Configure thePersistentObjectthat this instance will operate on.Required property.
- Parameters:
persistentObject- associated persistent object database
-
getXAResource
Get theXAResourcecorresponding 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- ifnameis nullIllegalStateException- if the current thread is not running within a transaction managed by aPersistentObjectTransactionManagerassigned 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 aPersistentObjectTransactionManagertransactionIllegalStateException- there is more than onePersistentObjectTransactionManagertransaction 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 togetRootwill 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- ifrootis null
-
getResourceFactory
- Specified by:
getResourceFactoryin interfaceResourceTransactionManager
-
doGetTransaction
- Specified by:
doGetTransactionin classAbstractPlatformTransactionManager
-
isExistingTransaction
- Overrides:
isExistingTransactionin classAbstractPlatformTransactionManager
-
doBegin
- Specified by:
doBeginin classAbstractPlatformTransactionManager
-
doSuspend
Suspend the current transaction.- Overrides:
doSuspendin classAbstractPlatformTransactionManager
-
doResume
Resume a previously suspended transaction.- Overrides:
doResumein classAbstractPlatformTransactionManager
-
prepareForCommit
- Overrides:
prepareForCommitin classAbstractPlatformTransactionManager
-
doCommit
- Specified by:
doCommitin classAbstractPlatformTransactionManager
-
doRollback
- Specified by:
doRollbackin classAbstractPlatformTransactionManager
-
doSetRollbackOnly
- Overrides:
doSetRollbackOnlyin classAbstractPlatformTransactionManager
-
doCleanupAfterCompletion
- Overrides:
doCleanupAfterCompletionin classAbstractPlatformTransactionManager
-