You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Guides/Transactions.md
+18-9Lines changed: 18 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,22 @@ Transactions in the driver must be started on a `ClientSession` using `startTran
8
8
9
9
## Examples
10
10
11
+
Below are some basic examples of using transactions in `MongoSwift`. In realistic use cases, transactions would ideally be retried when facing transient errors. For more detailed examples featuring retry logic, see the [official MongoDB documentation's examples](https://docs.mongodb.com/manual/core/transactions-in-applications/#txn-core-api).
12
+
11
13
### Transaction that Atomically Moves a `Document` from One `MongoCollection` to Another
12
14
13
15
The transaction below atomically deletes the document `{ "hello": "world" }` from the collection `test.src` and inserts the document in the collection `test.dest`. This ensures that the document exists in either `test.src` or `test.dest`, but not both or neither. Executing the delete and insert non-atomically raises the following issues:
14
16
- A race between `deleteOne()` and `insertOne()` where the document does not exist in either collection.
15
17
- If `deleteOne()` fails and `insertOne()` succeeds, the document exists in both collections.
16
18
- If `deleteOne()` succeeds and `insertOne()` fails, the document does not exist in either collection.
17
19
20
+
In order to achieve the highest safety guarantees that MongoDB transactions offer, a "snapshot" read concern and a "majority" write concern must be used. To see the varying levels safety provided by different read concern / write concern configurations, see the [official MongoDB documentation](https://docs.mongodb.com/manual/core/transactions/#read-concern-write-concern-read-preference).
21
+
22
+
Transactions will inherit the read concern / write concern / read preference specified on the client that started the transaction's session unless they were also specified in either the default transaction options or in the transaction options passed to `startTransaction`. See the below sections on how to do either.
23
+
24
+
**Note:** All operations executed as part of a transaction will use the transaction's read concern / write concern / read preference. Any of those options specified on the database or collection that executes the operation or on a per-operation basis will be _ignored_.
18
25
```swift
19
-
let client =tryMongoClient(using: elg)
26
+
let client =tryMongoClient(using: elg, options: ClientOptions(readConcern: .snapshot, writeConcern: .majority))
20
27
let session = client.startSession()
21
28
22
29
let db = client.db("test")
@@ -43,9 +50,9 @@ The default transaction options specified below apply to any transaction started
43
50
```swift
44
51
let txnOpts =TransactionOptions(
45
52
maxCommitTimeMS: 30,
46
-
readConcern: ReadConcern(.local),
47
-
readPreference: .primaryPreferred,
48
-
writeConcern: tryWriteConcern(w: .majority)
53
+
readConcern: .snapshot,
54
+
readPreference: .primary,
55
+
writeConcern: .majority
49
56
)
50
57
51
58
let client =tryMongoClient(using: elg)
@@ -63,17 +70,17 @@ session.startTransaction().flatMap { _ in
63
70
64
71
### Transaction with Custom Transaction Options
65
72
66
-
**Note**:: Any transaction options provided directly to `startTransaction()` override the default transaction options for the session. More so, the default transaction options for the session override any options inherited from the client.
73
+
**Note**: Any transaction options provided directly to `startTransaction()` override the default transaction options for the session. More so, the default transaction options for the session override any options inherited from the client.
0 commit comments