Skip to content

Commit 87e5f86

Browse files
committed
first draft
1 parent 137151e commit 87e5f86

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

source/crud/transactions.txt

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,93 @@ 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::
52+
53+
var client = new MongoClient("mongodb://localhost:27017");
54+
var session = client.StartSession();
55+
4556
.. warning::
4657

4758
Use an ``IClientSession`` only with the ``MongoClient`` (or associated
4859
``MongoDatabase`` or ``MongoCollection``) that created it. Using an
4960
``IClientSession`` with a different ``MongoClient`` results in operation
5061
errors.
5162

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+
52117
Causal Consistency
53118
~~~~~~~~~~~~~~~~~~
54119

0 commit comments

Comments
 (0)