1
1
.. _csharp-transactions:
2
2
3
- ============
4
- Transactions
5
- ============
3
+ ================================
4
+ Batch Operations in Transactions
5
+ ================================
6
6
7
7
.. facet::
8
8
:name: genre
9
9
:values: reference
10
10
11
11
.. meta::
12
- :keywords: code example, multi-document
12
+ :keywords: code example, multi-document, atomic, acid
13
13
14
14
.. contents:: On this page
15
15
:local:
@@ -27,28 +27,95 @@ transaction is committed. If any operation in the transaction returns an
27
27
error, the driver cancels the transaction and discards all data changes
28
28
before they ever become visible.
29
29
30
+ MongoDB guarantees that the data involved in your transaction operations remains
31
+ consistent, even if the operations encounter unexpected errors.
32
+
33
+ Sessions
34
+ --------
35
+
30
36
In MongoDB, transactions run within logical **sessions**. A
31
37
:manual:`session </reference/server-sessions/>` is a grouping of related
32
38
read or write operations that you intend to run sequentially. Sessions
33
- enable :manual:`causal consistency
34
- </core/read-isolation-consistency-recency/#causal-consistency>` for a
39
+ enable causal consistency for a
35
40
group of operations or allow you to execute operations in an
36
- :website:`ACID transaction </basics/acid-transactions>`. MongoDB
37
- guarantees that the data involved in your transaction operations remains
38
- consistent, even if the operations encounter unexpected errors.
41
+ :website:`ACID transaction </basics/acid-transactions>`.
39
42
40
43
When using the {+driver-short+}, you can create a new session from a
41
44
``MongoClient`` instance as an ``IClientSession`` type. We recommend that you reuse
42
45
your client for multiple sessions and transactions instead of
43
46
instantiating a new client each time.
44
47
48
+ The following example shows how to create a session by calling the ``StartSession()``
49
+ method:
50
+
51
+ .. code-block:: csharp
52
+ :copyable: true
53
+
54
+ var client = new MongoClient("mongodb://localhost:27017");
55
+ var session = client.StartSession();
56
+
45
57
.. warning::
46
58
47
59
Use an ``IClientSession`` only with the ``MongoClient`` (or associated
48
60
``MongoDatabase`` or ``MongoCollection``) that created it. Using an
49
61
``IClientSession`` with a different ``MongoClient`` results in operation
50
62
errors.
51
63
64
+ ClientSessionOptions
65
+ ~~~~~~~~~~~~~~~~~~~~
66
+
67
+ You can customize the behavior of your session by passing an instance of the
68
+ ``ClientSessionOptions`` class to the ``StartSession()`` method. The following table
69
+ describes the properties that you can set on a ``ClientSessionOptions`` object:
70
+
71
+ .. list-table::
72
+ :widths: 35 65
73
+ :header-rows: 1
74
+
75
+ * - Property
76
+ - Description
77
+
78
+ * - ``CausalConsistency``
79
+ - | Specifies whether the session is causally consistent. In a causally consistent session,
80
+ the driver executes operations in the order they were issued. To learn more, see
81
+ :ref:`<csharp-causal-consistency>`.
82
+ |
83
+ | **Data Type**: {+bool-data-type+}
84
+ | **Default**: ``true``
85
+
86
+ * - ``DefaultTransactionOptions``
87
+ - | Specifies the default transaction options for the session. This includes the maximum commit
88
+ time, read concern, read preference, and write concern.
89
+ |
90
+ | **Data Type**: `TransactionOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.TransactionOptions.html>`__
91
+ | **Default**: ``null``
92
+
93
+ * - ``Snapshot``
94
+ - | Specifies whether the driver performs snapshot reads. To learn more about snapshot
95
+ reads, see :manual:`Read Concern "snapshot" </reference/read-concern-snapshot/>`
96
+ in the {+mdb-server+} manual.
97
+ |
98
+ | **Data Type**: {+bool-data-type+}
99
+ | **Default**: ``false``
100
+
101
+ The following code example shows how to create a session with custom options:
102
+
103
+ .. code-block:: csharp
104
+ :copyable: true
105
+
106
+ var client = new MongoClient("mongodb://localhost:27017");
107
+ var sessionOptions = new ClientSessionOptions
108
+ {
109
+ CausalConsistency = true,
110
+ DefaultTransactionOptions = new TransactionOptions(
111
+ readConcern: ReadConcern.Available,
112
+ writeConcern: WriteConcern.Acknowledged)
113
+ };
114
+
115
+ var session = client.StartSession(sessionOptions);
116
+
117
+ .. _csharp-causal-consistency:
118
+
52
119
Causal Consistency
53
120
~~~~~~~~~~~~~~~~~~
54
121
@@ -94,7 +161,7 @@ tabs to learn about the methods to manage your transaction:
94
161
:tabid: synchronous-methods
95
162
96
163
.. list-table::
97
- :widths: 40 60
164
+ :widths: 30 70
98
165
:header-rows: 1
99
166
100
167
* - Method
0 commit comments