@@ -27,28 +27,93 @@ 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::
52
+
53
+ var client = new MongoClient("mongodb://localhost:27017");
54
+ var session = client.StartSession();
55
+
45
56
.. warning::
46
57
47
58
Use an ``IClientSession`` only with the ``MongoClient`` (or associated
48
59
``MongoDatabase`` or ``MongoCollection``) that created it. Using an
49
60
``IClientSession`` with a different ``MongoClient`` results in operation
50
61
errors.
51
62
63
+ ClientSessionOptions
64
+ ~~~~~~~~~~~~~~~~~~~~
65
+
66
+ You can customize the behavior of your session by passing an instance of the
67
+ ``ClientSessionOptions`` class to the ``StartSession()`` method. The following table
68
+ describes the properties that you can set on a ``ClientSessionOptions`` object:
69
+
70
+ .. list-table::
71
+ :widths: 20 80
72
+ :header-rows: 1
73
+
74
+ * - Property
75
+ - Description
76
+
77
+ * - ``CausalConsistency``
78
+ - Specifies whether the session is causally consistent. In a causally consistent session,
79
+ the driver executes operations in the order they were issued. To learn more, see
80
+ :ref:`<csharp-causal-consistency>`.
81
+ |
82
+ | **Data Type**: {+bool-data-type+}
83
+ | **Default**: ``true``
84
+
85
+ * - ``DefaultTransactionOptions``
86
+ - The default transaction options for the session. This includes the maximum commit
87
+ time, read concern, read preference, and write concern.
88
+ |
89
+ | **Data Type**: `TransactionOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.TransactionOptions.html>`__
90
+ | **Default**: ``null``
91
+
92
+ * - ``Snapshot``
93
+ - Specifies whether the driver performs snapshot reads. To learn more about snapshot
94
+ reads, see :manual:`Read Concern "snapshot" </reference/read-concern-snapshot/>`
95
+ in the {+mdb-server+} manual.
96
+ |
97
+ | **Data Type**: {+bool-data-type+}
98
+ | **Default**: ``false``
99
+
100
+ The following code example shows how to create a session with custom options:
101
+
102
+ .. code-block::
103
+
104
+ var client = new MongoClient("mongodb://localhost:27017");
105
+ var sessionOptions = new ClientSessionOptions
106
+ {
107
+ CausalConsistency = true,
108
+ DefaultTransactionOptions = new TransactionOptions(
109
+ readConcern: ReadConcern.Available,
110
+ writeConcern: WriteConcern.Acknowledged)
111
+ };
112
+
113
+ var session = client.StartSession(sessionOptions);
114
+
115
+ .. _csharp-causal-consistency:
116
+
52
117
Causal Consistency
53
118
~~~~~~~~~~~~~~~~~~
54
119
0 commit comments