#include <userver/storages/mongo/transaction.hpp>
MongoDB transaction handle that provides ACID guarantees.
Transactions group multiple operations within logical units of work and provide ACID guarantees. All operations in a transaction either succeed completely or fail completely.
A transaction pins exactly one connection from the pool starting from the first call to storages::mongo::Transaction::GetCollection and holds it until the transaction completes, storages::mongo::Transaction::Abort is called, or the transaction is destroyed. An unknown commit result retains the connection for another commit attempt.
Definition at line 39 of file transaction.hpp.
Public Types | |
| enum class | State { kNone , kInProgress , kCommitted , kAborted , kCommitUnknown } |
| Get the transaction state. More... | |
Public Member Functions | |
| Transaction () | |
| Creates invalid transaction. | |
| Transaction (Transaction &&) noexcept | |
| Move constructor. | |
| Transaction & | operator= (Transaction &&) noexcept |
| Move assignment operator. | |
| ~Transaction () | |
| Aborts an active transaction or releases an unresolved commit session. | |
| Collection | GetCollection (std::string name) |
| Get a collection handle within the transaction context. | |
| void | Commit () |
| Commit the transaction. | |
| void | Abort () noexcept |
| Abort the transaction. | |
| bool | IsActive () const noexcept |
| Check whether the transaction is in progress. | |
| State | GetState () const noexcept |
| Get current transaction state. | |
|
strong |
Get the transaction state.
| Enumerator | |
|---|---|
| kNone | Transaction not started. |
| kInProgress | Transaction is active. |
| kCommitted | Transaction has been committed. |
| kAborted | Transaction has been aborted. |
| kCommitUnknown | Commit outcome is unknown; collection operations are forbidden. |
Definition at line 92 of file transaction.hpp.
|
noexcept |
Abort the transaction.
Discards all changes made within the transaction. This method is idempotent and safe to call multiple times. If the commit result is unknown, releases the session without reporting a rollback or changing storages::mongo::Transaction::State::kCommitUnknown. Commit cannot be retried after the session has been released.
| void storages::mongo::Transaction::Commit | ( | ) |
Commit the transaction.
Makes all changes within the transaction permanent and visible to other operations. If the transaction fails to commit, an exception will be thrown.
| MongoException | on commit failure |
| storages::mongo::TransactionException | with storages::mongo::TransactionException::Type::kUnknownCommitResult if the outcome is unknown. Retains the session for another commit attempt; collection operations are forbidden until the outcome is resolved. |
| Collection storages::mongo::Transaction::GetCollection | ( | std::string | name | ) |
Get a collection handle within the transaction context.
| name | collection name |
|
noexcept |
Check whether the transaction is in progress.