Class MongoSynchronousTransactionManagerImpl
- All Implemented Interfaces:
MongoSynchronousTransactionManager,TransactionStateOperations<com.mongodb.client.ClientSession,,SynchronousTransactionState> SynchronousTransactionManager<com.mongodb.client.ClientSession>,TransactionManager,TransactionOperations<com.mongodb.client.ClientSession>,Serializable
- Since:
- 3.3
- Author:
- Denis Stepanov
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class io.micronaut.transaction.support.AbstractSynchronousTransactionManager
AbstractSynchronousTransactionManager.SuspendedResourcesHolder -
Field Summary
Fields inherited from class io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager
logger -
Constructor Summary
ConstructorsConstructorDescriptionMongoSynchronousTransactionManagerImpl(com.mongodb.client.MongoClient mongoClient, String name) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidClose existing connection.protected voiddoBegin(Object transaction, TransactionDefinition definition) Begin a new transaction with semantics according to the given transaction definition.protected voiddoCleanupAfterCompletion(Object transaction) Cleanup resources after transaction completion.protected voiddoCommit(DefaultTransactionStatus<com.mongodb.client.ClientSession> status) Perform an actual commit of the given transaction.protected ObjectReturn a transaction object for the current transaction state.protected voidResume the resources of the current transaction.protected voiddoRollback(DefaultTransactionStatus<com.mongodb.client.ClientSession> status) Perform an actual rollback of the given transaction.protected voidSet the given transaction rollback-only.protected ObjectSuspend the resources of the current transaction.com.mongodb.client.ClientSessionFind existing connection.com.mongodb.client.MongoClientcom.mongodb.client.ClientSessionObtains the connection for the current transaction.protected com.mongodb.client.ClientSessiongetConnection(Object transaction) The connection for the given transaction object.protected ObjectGet the transaction state key that should be used to store the state.booleanCheck if the connection exists.protected booleanisExistingTransaction(Object transaction) Check if the given transaction object indicates an existing transaction (that is, a transaction which has already started).Methods inherited from class io.micronaut.transaction.support.AbstractSynchronousTransactionManager
commit, doCleanupAfterCompletion, doDestroyState, doGetTransaction, execute, executeRead, executeWrite, find, findOrCreateState, getConnection, getState, getTransaction, invokeAfterCompletion, isExistingTransaction, newTransactionStatus, newTransactionStatus, prepareSynchronization, prepareSynchronization, prepareTransactionStatus, registerAfterCompletionWithExistingTransaction, registerAfterCompletionWithExistingTransaction, resume, rollback, suspend, triggerBeforeCommit, triggerBeforeCompletionMethods inherited from class io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager
commit, determineTimeout, execute, executeRead, executeWrite, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, prepareForCommit, prepareTransactionStatus, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, 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 io.micronaut.transaction.SynchronousTransactionManager
commit, getTransaction, rollbackMethods inherited from interface io.micronaut.transaction.TransactionOperations
execute, executeRead, executeWrite
-
Constructor Details
-
MongoSynchronousTransactionManagerImpl
public MongoSynchronousTransactionManagerImpl(com.mongodb.client.MongoClient mongoClient, @Nullable @Parameter String name) Default constructor.- Parameters:
mongoClient- The mongo clientname- The datasource name
-
-
Method Details
-
getClient
public com.mongodb.client.MongoClient getClient()- Specified by:
getClientin interfaceMongoSynchronousTransactionManager- Returns:
- The mongo client
-
getTransactionStateKey
Description copied from class:AbstractSynchronousTransactionManagerGet the transaction state key that should be used to store the state.- Overrides:
getTransactionStateKeyin classAbstractSynchronousTransactionManager<com.mongodb.client.ClientSession>- Returns:
- The key
-
findClientSession
@Nullable public com.mongodb.client.ClientSession findClientSession()Find existing connection.- Returns:
- The client session
-
closeClientSession
public void closeClientSession()Close existing connection. -
getConnection
public com.mongodb.client.ClientSession getConnection()Description copied from interface:TransactionOperationsObtains the connection for the current transaction.- Specified by:
getConnectionin interfaceTransactionOperations<com.mongodb.client.ClientSession>- Returns:
- The connection
-
hasConnection
public boolean hasConnection()Description copied from interface:TransactionOperationsCheck if the connection exists.- Specified by:
hasConnectionin interfaceTransactionOperations<com.mongodb.client.ClientSession>- Returns:
- True if transaction exists
-
getConnection
Description copied from class:AbstractSynchronousTransactionManagerThe connection for the given transaction object.- Specified by:
getConnectionin classAbstractSynchronousTransactionManager<com.mongodb.client.ClientSession>- Parameters:
transaction- The transaction- Returns:
- The connection.
-
doGetTransaction
Description copied from class:AbstractSynchronousTransactionManagerReturn a transaction object for the current transaction state.The returned object will usually be specific to the concrete transaction manager implementation, carrying corresponding transaction state in a modifiable fashion. This object will be passed into the other template methods (e.g. doBegin and doCommit), either directly or as part of a DefaultTransactionStatus instance.
The returned object should contain information about any existing transaction, that is, a transaction that has already started before the current
getTransactioncall on the transaction manager. Consequently, adoGetTransactionimplementation will usually look for an existing transaction and store corresponding state in the returned transaction object.- Specified by:
doGetTransactionin classAbstractSynchronousTransactionManager<com.mongodb.client.ClientSession>- Returns:
- the current transaction object
- Throws:
CannotCreateTransactionException- if transaction support is not availableTransactionException- in case of lookup or system errors- See Also:
-
AbstractSynchronousStateTransactionManager.doBegin(java.lang.Object, io.micronaut.transaction.TransactionDefinition)AbstractSynchronousStateTransactionManager.doCommit(io.micronaut.transaction.support.DefaultTransactionStatus<T>)AbstractSynchronousStateTransactionManager.doRollback(io.micronaut.transaction.support.DefaultTransactionStatus<T>)DefaultTransactionStatus.getTransaction()
-
isExistingTransaction
Description copied from class:AbstractSynchronousTransactionManagerCheck if the given transaction object indicates an existing transaction (that is, a transaction which has already started).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.- Overrides:
isExistingTransactionin classAbstractSynchronousTransactionManager<com.mongodb.client.ClientSession>- Parameters:
transaction- transaction object returned by doGetTransaction- Returns:
- if there is an existing transaction
- Throws:
TransactionException- in case of system errors- See Also:
-
doBegin
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException Description copied from class:AbstractSynchronousStateTransactionManagerBegin a new transaction with semantics according to the given transaction definition. Does not have to care about applying the propagation behavior, as this has already been handled by this abstract manager.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.- Specified by:
doBeginin classAbstractSynchronousStateTransactionManager<com.mongodb.client.ClientSession>- Parameters:
transaction- transaction object returned bydoGetTransactiondefinition- a TransactionDefinition instance, describing propagation behavior, isolation level, read-only flag, timeout, and transaction name- Throws:
TransactionException- in case of creation or system errorsNestedTransactionNotSupportedException- if the underlying transaction does not support nesting
-
doCommit
protected void doCommit(DefaultTransactionStatus<com.mongodb.client.ClientSession> status) throws TransactionException Description copied from class:AbstractSynchronousStateTransactionManagerPerform an actual commit of the given transaction.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.
- Specified by:
doCommitin classAbstractSynchronousStateTransactionManager<com.mongodb.client.ClientSession>- Parameters:
status- the status representation of the transaction- Throws:
TransactionException- in case of commit or system errors- See Also:
-
doRollback
protected void doRollback(DefaultTransactionStatus<com.mongodb.client.ClientSession> status) throws TransactionException Description copied from class:AbstractSynchronousStateTransactionManagerPerform an actual rollback of the given transaction.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.
- Specified by:
doRollbackin classAbstractSynchronousStateTransactionManager<com.mongodb.client.ClientSession>- Parameters:
status- the status representation of the transaction- Throws:
TransactionException- in case of system errors- See Also:
-
doSuspend
Description copied from class:AbstractSynchronousStateTransactionManagerSuspend the resources of the current transaction. Transaction synchronization will already have been suspended.The default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.
- Overrides:
doSuspendin classAbstractSynchronousStateTransactionManager<com.mongodb.client.ClientSession>- Parameters:
transaction- transaction object returned bydoGetTransaction- Returns:
- an object that holds suspended resources (will be kept unexamined for passing it into doResume)
- See Also:
-
doResume
Description copied from class:AbstractSynchronousStateTransactionManagerResume the resources of the current transaction. Transaction synchronization will be resumed afterwards.The default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.
- Overrides:
doResumein classAbstractSynchronousStateTransactionManager<com.mongodb.client.ClientSession>- Parameters:
transaction- transaction object returned bydoGetTransactionsuspendedResources- the object that holds suspended resources, as returned by doSuspend- See Also:
-
doSetRollbackOnly
Description copied from class:AbstractSynchronousStateTransactionManagerSet the given transaction rollback-only. Only called on rollback if the current transaction participates in an existing one.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.
- Overrides:
doSetRollbackOnlyin classAbstractSynchronousStateTransactionManager<com.mongodb.client.ClientSession>- Parameters:
status- the status representation of the transaction
-
doCleanupAfterCompletion
Description copied from class:AbstractSynchronousTransactionManagerCleanup resources after transaction completion.Called after
doCommitanddoRollbackexecution, on any outcome. The default implementation does nothing.Should not throw any exceptions but just issue warnings on errors.
- Overrides:
doCleanupAfterCompletionin classAbstractSynchronousTransactionManager<com.mongodb.client.ClientSession>- Parameters:
transaction- transaction object returned bydoGetTransaction
-