1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::time::Duration;
use crate::{
client::options::TransactionOptions,
options::{ReadConcern, WriteConcern},
selection_criteria::SelectionCriteria,
ClientSession,
};
use super::{export_doc, option_setters, options_doc};
impl ClientSession {
/// Starts a new transaction on this session. If no options are set, the session's
/// `defaultTransactionOptions` will be used. This session must be passed into each operation
/// within the transaction; otherwise, the operation will be executed outside of the
/// transaction.
///
/// Errors returned from operations executed within a transaction may include a
/// [`crate::error::TRANSIENT_TRANSACTION_ERROR`] label. This label indicates that the entire
/// transaction can be retried with a reasonable expectation that it will succeed.
///
/// ```rust
/// # use mongodb::{bson::{doc, Document}, error::Result, Client, ClientSession};
/// #
/// # async fn do_stuff() -> Result<()> {
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
/// # let coll = client.database("foo").collection::<Document>("bar");
/// # let mut session = client.start_session().await?;
/// session.start_transaction().await?;
/// let result = coll.insert_one(doc! { "x": 1 }).session(&mut session).await?;
/// session.commit_transaction().await?;
/// # Ok(())
/// # }
/// ```
///
/// `await` will return [`Result<()>`].
#[options_doc(start_transaction)]
pub fn start_transaction(&mut self) -> StartTransaction<&mut Self> {
StartTransaction {
session: self,
options: None,
}
}
/// Commits the transaction that is currently active on this session.
///
/// This method may return an error with a [`crate::error::UNKNOWN_TRANSACTION_COMMIT_RESULT`]
/// label. This label indicates that it is unknown whether the commit has satisfied the write
/// concern associated with the transaction. If an error with this label is returned, it is
/// safe to retry the commit until the write concern is satisfied or an error without the label
/// is returned.
///
/// ```rust
/// # use mongodb::{bson::{doc, Document}, error::Result, Client, ClientSession};
/// #
/// # async fn do_stuff() -> Result<()> {
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
/// # let coll = client.database("foo").collection::<Document>("bar");
/// # let mut session = client.start_session().await?;
/// session.start_transaction().await?;
/// let result = coll.insert_one(doc! { "x": 1 }).session(&mut session).await?;
/// session.commit_transaction().await?;
/// # Ok(())
/// # }
/// ```
///
/// This operation will retry once upon failure if the connection and encountered error support
/// retryability. See the documentation
/// [here](https://www.mongodb.com/docs/manual/core/retryable-writes/) for more information on
/// retryable writes.
///
/// `await` will return [`Result<()>`].
pub fn commit_transaction(&mut self) -> CommitTransaction {
CommitTransaction { session: self }
}
/// Aborts the transaction that is currently active on this session. Any open transaction will
/// be aborted automatically in the `Drop` implementation of `ClientSession`.
///
/// ```rust
/// # use mongodb::{bson::{doc, Document}, error::Result, Client, ClientSession, Collection};
/// #
/// # async fn do_stuff() -> Result<()> {
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
/// # let coll = client.database("foo").collection::<Document>("bar");
/// # let mut session = client.start_session().await?;
/// session.start_transaction().await?;
/// match execute_transaction(&coll, &mut session).await {
/// Ok(_) => session.commit_transaction().await?,
/// Err(_) => session.abort_transaction().await?,
/// }
/// # Ok(())
/// # }
///
/// async fn execute_transaction(coll: &Collection<Document>, session: &mut ClientSession) -> Result<()> {
/// coll.insert_one(doc! { "x": 1 }).session(&mut *session).await?;
/// coll.delete_one(doc! { "y": 2 }).session(&mut *session).await?;
/// Ok(())
/// }
/// ```
///
/// This operation will retry once upon failure if the connection and encountered error support
/// retryability. See the documentation
/// [here](https://www.mongodb.com/docs/manual/core/retryable-writes/) for more information on
/// retryable writes.
///
/// `await` will return [`Result<()>`].
pub fn abort_transaction(&mut self) -> AbortTransaction {
AbortTransaction { session: self }
}
}
#[cfg(feature = "sync")]
impl crate::sync::ClientSession {
/// Starts a new transaction on this session with the given `TransactionOptions`. If no options
/// are provided, the session's `defaultTransactionOptions` will be used. This session must
/// be passed into each operation within the transaction; otherwise, the operation will be
/// executed outside of the transaction.
///
/// ```rust
/// # use mongodb::{bson::{doc, Document}, error::Result, sync::{Client, ClientSession}};
/// #
/// # async fn do_stuff() -> Result<()> {
/// # let client = Client::with_uri_str("mongodb://example.com")?;
/// # let coll = client.database("foo").collection::<Document>("bar");
/// # let mut session = client.start_session().run()?;
/// session.start_transaction().run()?;
/// let result = coll.insert_one(doc! { "x": 1 }).session(&mut session).run()?;
/// session.commit_transaction().run()?;
/// # Ok(())
/// # }
/// ```
///
/// [`run`](StartTransaction::run) will return [`Result<()>`].
#[options_doc(start_transaction, sync)]
pub fn start_transaction(&mut self) -> StartTransaction<&mut Self> {
StartTransaction {
session: self,
options: None,
}
}
/// Commits the transaction that is currently active on this session.
///
/// ```rust
/// # use mongodb::{bson::{doc, Document}, error::Result, sync::{Client, ClientSession}};
/// #
/// # async fn do_stuff() -> Result<()> {
/// # let client = Client::with_uri_str("mongodb://example.com")?;
/// # let coll = client.database("foo").collection::<Document>("bar");
/// # let mut session = client.start_session().run()?;
/// session.start_transaction().run()?;
/// let result = coll.insert_one(doc! { "x": 1 }).session(&mut session).run()?;
/// session.commit_transaction().run()?;
/// # Ok(())
/// # }
/// ```
///
/// This operation will retry once upon failure if the connection and encountered error support
/// retryability. See the documentation
/// [here](https://www.mongodb.com/docs/manual/core/retryable-writes/) for more information on
/// retryable writes.
///
/// [`run`](CommitTransaction::run) will return [`Result<()>`].
pub fn commit_transaction(&mut self) -> CommitTransaction {
self.async_client_session.commit_transaction()
}
/// Aborts the transaction that is currently active on this session. Any open transaction will
/// be aborted automatically in the `Drop` implementation of `ClientSession`.
///
/// ```rust
/// # use mongodb::{bson::{doc, Document}, error::Result, sync::{Client, ClientSession, Collection}};
/// #
/// # async fn do_stuff() -> Result<()> {
/// # let client = Client::with_uri_str("mongodb://example.com")?;
/// # let coll = client.database("foo").collection::<Document>("bar");
/// # let mut session = client.start_session().run()?;
/// session.start_transaction().run()?;
/// match execute_transaction(coll, &mut session) {
/// Ok(_) => session.commit_transaction().run()?,
/// Err(_) => session.abort_transaction().run()?,
/// }
/// # Ok(())
/// # }
///
/// fn execute_transaction(coll: Collection<Document>, session: &mut ClientSession) -> Result<()> {
/// coll.insert_one(doc! { "x": 1 }).session(&mut *session).run()?;
/// coll.delete_one(doc! { "y": 2 }).session(&mut *session).run()?;
/// Ok(())
/// }
/// ```
///
/// This operation will retry once upon failure if the connection and encountered error support
/// retryability. See the documentation
/// [here](https://www.mongodb.com/docs/manual/core/retryable-writes/) for more information on
/// retryable writes.
///
/// [`run`](AbortTransaction::run) will return [`Result<()>`].
pub fn abort_transaction(&mut self) -> AbortTransaction {
self.async_client_session.abort_transaction()
}
}
/// Start a new transaction. Construct with [`ClientSession::start_transaction`].
#[must_use]
pub struct StartTransaction<S> {
pub(crate) session: S,
pub(crate) options: Option<TransactionOptions>,
}
#[option_setters(crate::client::options::TransactionOptions)]
#[export_doc(start_transaction)]
impl<S> StartTransaction<S> {}
/// Commits a currently-active transaction. Construct with [`ClientSession::commit_transaction`].
#[must_use]
pub struct CommitTransaction<'a> {
pub(crate) session: &'a mut ClientSession,
}
/// Abort the currently active transaction on a session. Construct with
/// [`ClientSession::abort_transaction`].
#[must_use]
pub struct AbortTransaction<'a> {
pub(crate) session: &'a mut ClientSession,
}
// Action impls at src/client/session/action.rs