Skip to content

Commit 60e0cca

Browse files
authored
DOCSP-49811 - ClientSessionOptions (#627)
1 parent 137151e commit 60e0cca

File tree

1 file changed

+77
-10
lines changed

1 file changed

+77
-10
lines changed

source/crud/transactions.txt

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
.. _csharp-transactions:
22

3-
============
4-
Transactions
5-
============
3+
================================
4+
Batch Operations in Transactions
5+
================================
66

77
.. facet::
88
:name: genre
99
:values: reference
1010

1111
.. meta::
12-
:keywords: code example, multi-document
12+
:keywords: code example, multi-document, atomic, acid
1313

1414
.. contents:: On this page
1515
:local:
@@ -27,28 +27,95 @@ transaction is committed. If any operation in the transaction returns an
2727
error, the driver cancels the transaction and discards all data changes
2828
before they ever become visible.
2929

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+
3036
In MongoDB, transactions run within logical **sessions**. A
3137
:manual:`session </reference/server-sessions/>` is a grouping of related
3238
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
3540
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>`.
3942

4043
When using the {+driver-short+}, you can create a new session from a
4144
``MongoClient`` instance as an ``IClientSession`` type. We recommend that you reuse
4245
your client for multiple sessions and transactions instead of
4346
instantiating a new client each time.
4447

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+
4557
.. warning::
4658

4759
Use an ``IClientSession`` only with the ``MongoClient`` (or associated
4860
``MongoDatabase`` or ``MongoCollection``) that created it. Using an
4961
``IClientSession`` with a different ``MongoClient`` results in operation
5062
errors.
5163

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+
52119
Causal Consistency
53120
~~~~~~~~~~~~~~~~~~
54121

@@ -94,7 +161,7 @@ tabs to learn about the methods to manage your transaction:
94161
:tabid: synchronous-methods
95162

96163
.. list-table::
97-
:widths: 40 60
164+
:widths: 30 70
98165
:header-rows: 1
99166

100167
* - Method

0 commit comments

Comments
 (0)