userver: storages::mongo::Transaction Class Reference
Loading...
Searching...
No Matches
storages::mongo::Transaction Class Reference

#include <userver/storages/mongo/transaction.hpp>

Detailed Description

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.

Usage example:

auto txn = GetDefaultPool().BeginTransaction();
auto collection = txn.GetCollection(kCollectionName);
// Insert multiple documents
std::vector<formats::bson::Document> docs;
for (int i = 0; i < 5; ++i) {
docs.push_back(bson::MakeDoc("name", fmt::format("user_{}", i), "age", 20 + i));
}
auto insert_result = collection.InsertMany(docs);
EXPECT_EQ(insert_result.InsertedCount(), 5);
// Update one document
auto filter = bson::MakeDoc("name", "user_2");
auto update = bson::MakeDoc("$set", bson::MakeDoc("age", 100));
auto update_result = collection.UpdateOne(filter, update);
EXPECT_EQ(update_result.ModifiedCount(), 1);
// Delete one document
auto delete_filter = bson::MakeDoc("name", "user_4");
auto delete_result = collection.DeleteOne(delete_filter);
EXPECT_EQ(delete_result.DeletedCount(), 1);
// Commit all operations
txn.Commit();
Note
Transactions require MongoDB replica sets or sharded clusters.
Operations within a transaction are automatically retried according to MongoDB transaction retry semantics.

Definition at line 33 of file transaction.hpp.

Public Types

enum class  State {
  kNone ,
  kInProgress ,
  kCommitted ,
  kAborted
}
 Get the transaction state. More...
 

Public Member Functions

 Transaction ()
 Creates invalid transaction.
 
 Transaction (Transaction &&) noexcept
 Move constructor.
 
Transactionoperator= (Transaction &&) noexcept
 Move assignment operator.
 
 ~Transaction ()
 Destructor. Aborts transaction if not committed.
 
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 if transaction is active.
 
State GetState () const noexcept
 Get current transaction state.
 

Member Enumeration Documentation

◆ State

Get the transaction state.

Enumerator
kNone 

Transaction not started.

kInProgress 

Transaction is active.

kCommitted 

Transaction has been committed.

kAborted 

Transaction has been aborted.

Definition at line 77 of file transaction.hpp.

Member Function Documentation

◆ Abort()

void storages::mongo::Transaction::Abort ( )
noexcept

Abort the transaction.

Discards all changes made within the transaction. This method is idempotent and safe to call multiple times.

◆ Commit()

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.

Exceptions
MongoExceptionon commit failure

◆ GetCollection()

Collection storages::mongo::Transaction::GetCollection ( std::string  name)

Get a collection handle within the transaction context.

Parameters
namecollection name
Returns
Collection handle that operates within this transaction

◆ IsActive()

bool storages::mongo::Transaction::IsActive ( ) const
noexcept

Check if transaction is active.

Returns
true if transaction is active (not committed or aborted)

The documentation for this class was generated from the following file: