MongoDB C++ Driver mongocxx-3.10.1
Loading...
Searching...
No Matches
client_session.hpp
1// Copyright 2017-present MongoDB Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <functional>
18#include <memory>
19
20#include <mongocxx/bulk_write-fwd.hpp>
21#include <mongocxx/client-fwd.hpp>
22#include <mongocxx/client_session-fwd.hpp>
23#include <mongocxx/collection-fwd.hpp>
24#include <mongocxx/database-fwd.hpp>
25#include <mongocxx/index_view-fwd.hpp>
26#include <mongocxx/search_index_view-fwd.hpp>
27
28#include <bsoncxx/document/view.hpp>
29#include <bsoncxx/stdx/optional.hpp>
30#include <mongocxx/options/client_session.hpp>
31
32#include <mongocxx/config/prelude.hpp>
33
34namespace mongocxx {
35namespace v_noabi {
36
48 public:
49 enum class transaction_state {
50 k_transaction_none,
51 k_transaction_starting,
52 k_transaction_in_progress,
53 k_transaction_committed,
54 k_transaction_aborted,
55 };
56
61
65 client_session& operator=(client_session&&) noexcept;
66
67 client_session(const client_session&) = delete;
68 client_session& operator=(const client_session&) = delete;
69
73 ~client_session() noexcept;
74
78 const mongocxx::v_noabi::client& client() const noexcept;
79
83 const options::client_session& options() const noexcept;
84
89 bsoncxx::v_noabi::document::view id() const noexcept;
90
97 bsoncxx::v_noabi::document::view cluster_time() const noexcept;
98
104 bsoncxx::v_noabi::types::b_timestamp operation_time() const noexcept;
105
110 std::uint32_t server_id() const noexcept;
111
115 transaction_state get_transaction_state() const noexcept;
116
120 bool get_dirty() const noexcept;
121
130 void advance_cluster_time(const bsoncxx::v_noabi::document::view& cluster_time);
131
140 void advance_operation_time(const bsoncxx::v_noabi::types::b_timestamp& operation_time);
141
152 void start_transaction(const stdx::optional<options::transaction>& transaction_opts = {});
153
162
170
192 using with_transaction_cb = std::function<void MONGOCXX_CALL(client_session*)>;
193 void with_transaction(with_transaction_cb cb, options::transaction opts = {});
194
195 private:
196 friend ::mongocxx::v_noabi::bulk_write;
197 friend ::mongocxx::v_noabi::client;
198 friend ::mongocxx::v_noabi::collection;
199 friend ::mongocxx::v_noabi::database;
200 friend ::mongocxx::v_noabi::index_view;
201 friend ::mongocxx::v_noabi::search_index_view;
202
203 class MONGOCXX_PRIVATE impl;
204
205 MONGOCXX_PRIVATE client_session(const mongocxx::v_noabi::client* client,
207
208 MONGOCXX_PRIVATE impl& _get_impl();
209 MONGOCXX_PRIVATE const impl& _get_impl() const;
210
211 std::unique_ptr<impl> _impl;
212};
213
214} // namespace v_noabi
215} // namespace mongocxx
216
217#include <mongocxx/config/postlude.hpp>
218
Use a session for a sequence of operations, optionally with either causal consistency or snapshots.
Definition client_session.hpp:47
void advance_operation_time(const bsoncxx::v_noabi::types::b_timestamp &operation_time)
Advance the session's operation time, expressed as a BSON timestamp.
bsoncxx::v_noabi::document::view cluster_time() const noexcept
Get the session's clusterTime, as a BSON document.
transaction_state get_transaction_state() const noexcept
Returns the current transaction state for this session.
void advance_cluster_time(const bsoncxx::v_noabi::document::view &cluster_time)
Advance the cluster time for a session.
const options::client_session & options() const noexcept
Gets the options this session was created with.
client_session(client_session &&) noexcept
Move constructs a session.
bsoncxx::v_noabi::types::b_timestamp operation_time() const noexcept
Get the session's operationTime, as a BSON timestamp.
bool get_dirty() const noexcept
Returns whether or not this session is dirty.
void start_transaction(const stdx::optional< options::transaction > &transaction_opts={})
Starts a transaction on the current client session.
std::function< void(client_session *)> with_transaction_cb
Helper to run a user-provided callback within a transaction.
Definition client_session.hpp:192
void abort_transaction()
Aborts a transaction on the current client session.
std::uint32_t server_id() const noexcept
Get the server_id the session is pinned to.
void commit_transaction()
Commits a transaction on the current client session.
Class representing a client connection to MongoDB.
Definition client.hpp:61
Class representing the optional arguments to mongocxx::v_noabi::client::start_session.
Definition client_session.hpp:33
Class representing the optional arguments for a transaction.
Definition transaction.hpp:38
The top-level namespace for bsoncxx library entities.
Definition element-fwd.hpp:19
The top-level namespace for mongocxx library entities.
Definition bulk_write-fwd.hpp:19