userver: userver/storages/mongo/transaction.hpp Source File
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
12USERVER_NAMESPACE_BEGIN
13
14namespace storages::mongo {
15
16namespace impl {
17class 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 the transaction completes, @ref storages::mongo::Transaction::Abort
29/// is called, or the transaction is destroyed.
30/// An unknown commit result retains the connection for another commit attempt.
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 A transient transaction error requires retrying the whole transaction.
38/// An unknown commit result requires retrying commit on the same transaction.
40public:
41 /// @brief Creates invalid transaction
43
44 /// @cond
45 // For internal use only.
46 explicit Transaction(std::unique_ptr<impl::TransactionImpl>);
47 /// @endcond
48
49 /// @brief Move constructor
51
52 /// @brief Move assignment operator
54
55 /// @brief Aborts an active transaction or releases an unresolved commit session.
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 /// @throws storages::mongo::TransactionException with
71 /// @ref storages::mongo::TransactionException::Type::kUnknownCommitResult
72 /// if the outcome is unknown. Retains the session for another commit attempt;
73 /// collection operations are forbidden until the outcome is resolved.
74 void Commit();
75
76 /// @brief Abort the transaction
77 ///
78 /// Discards all changes made within the transaction.
79 /// This method is idempotent and safe to call multiple times.
80 /// If the commit result is unknown, releases the session without reporting
81 /// a rollback or changing @ref storages::mongo::Transaction::State::kCommitUnknown.
82 /// Commit cannot be retried after the session has been released.
83 void Abort() noexcept;
84
85 /// @brief Check whether the transaction is in progress.
86 /// @return true only in @ref storages::mongo::Transaction::State::kInProgress.
87 /// @note Returns false when the commit outcome is unknown.
88 /// Commit may still be retried while the session is retained.
89 bool IsActive() const noexcept;
90
91 /// @brief Get the transaction state
92 enum class State {
93 kNone, ///< Transaction not started
94 kInProgress, ///< Transaction is active
95 kCommitted, ///< Transaction has been committed
96 kAborted, ///< Transaction has been aborted
97 kCommitUnknown ///< Commit outcome is unknown; collection operations are forbidden
98 };
99
100 /// @brief Get current transaction state
101 State GetState() const noexcept;
102
103private:
104 std::unique_ptr<impl::TransactionImpl> impl_;
105};
106
107} // namespace storages::mongo
108
109USERVER_NAMESPACE_END