userver
C++ Async Framework
Loading...
Searching...
No Matches
transaction.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/storages/mongo/transaction.hpp
4
/// @brief @copybrief storages::mongo::Transaction
5
6
#
include
<
memory
>
7
#
include
<
string
>
8
9
#
include
<
userver
/
storages
/
mongo
/
collection
.
hpp
>
10
#
include
<
userver
/
storages
/
mongo
/
options
.
hpp
>
11
12
USERVER_NAMESPACE_BEGIN
13
14
namespace
storages::mongo {
15
16
namespace
impl {
17
class
TransactionImpl;
18
}
// namespace impl
19
20
/// @brief MongoDB transaction handle that provides ACID guarantees.
21
///
22
/// Transactions group multiple operations within logical units of work
23
/// and provide ACID guarantees. All operations in a transaction either
24
/// succeed completely or fail completely.
25
///
26
/// A transaction pins exactly one connection from the pool starting from the
27
/// first call to storages::mongo::Transaction::GetCollection and holds it
28
/// until storages::mongo::Transaction::Commit or
29
/// storages::mongo::Transaction::Abort is called (or the transaction is
30
/// destroyed).
31
///
32
/// ## Usage example:
33
///
34
/// @snippet mongo/src/storages/mongo/transaction_mongotest.cpp transaction
35
///
36
/// @note Transactions require MongoDB replica sets or sharded clusters.
37
/// @note Operations within a transaction are automatically retried according
38
/// to MongoDB transaction retry semantics.
39
class
Transaction
{
40
public
:
41
/// @brief Creates invalid transaction
42
Transaction
();
43
44
/// @cond
45
// For internal use only.
46
explicit
Transaction(std::unique_ptr<impl::TransactionImpl>);
47
/// @endcond
48
49
/// @brief Move constructor
50
Transaction
(
Transaction
&&)
noexcept
;
51
52
/// @brief Move assignment operator
53
Transaction
&
operator
=(
Transaction
&&)
noexcept
;
54
55
/// @brief Destructor. Aborts transaction if not committed.
56
~
Transaction
();
57
58
/// @brief Get a collection handle within the transaction context
59
/// @param name collection name
60
/// @return Collection handle that operates within this transaction
61
Collection
GetCollection
(std::string name);
62
63
/// @brief Commit the transaction
64
///
65
/// Makes all changes within the transaction permanent and visible
66
/// to other operations. If the transaction fails to commit,
67
/// an exception will be thrown.
68
///
69
/// @throws MongoException on commit failure
70
void
Commit
();
71
72
/// @brief Abort the transaction
73
///
74
/// Discards all changes made within the transaction.
75
/// This method is idempotent and safe to call multiple times.
76
void
Abort
()
noexcept
;
77
78
/// @brief Check if transaction is active
79
/// @return true if transaction is active (not committed or aborted)
80
bool
IsActive
()
const
noexcept
;
81
82
/// @brief Get the transaction state
83
enum
class
State
{
84
kNone
,
///< Transaction not started
85
kInProgress
,
///< Transaction is active
86
kCommitted
,
///< Transaction has been committed
87
kAborted
///< Transaction has been aborted
88
};
89
90
/// @brief Get current transaction state
91
State
GetState
()
const
noexcept
;
92
93
private
:
94
std::unique_ptr<impl::TransactionImpl> impl_;
95
};
96
97
}
// namespace storages::mongo
98
99
USERVER_NAMESPACE_END
userver
storages
mongo
transaction.hpp
Generated on Thu May 21 2026 16:21:21 for userver by
Doxygen
1.13.2