Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 74 additions & 9 deletions source/crud/transactions.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.. _csharp-transactions:

============
Transactions
============
================================
Batch Operations in Transactions
================================

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: code example, multi-document
:keywords: code example, multi-document, atomic, acid

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

MongoDB guarantees that the data involved in your transaction operations remains
consistent, even if the operations encounter unexpected errors.

Sessions
--------

In MongoDB, transactions run within logical **sessions**. A
:manual:`session </reference/server-sessions/>` is a grouping of related
read or write operations that you intend to run sequentially. Sessions
enable :manual:`causal consistency
</core/read-isolation-consistency-recency/#causal-consistency>` for a
enable causal consistency for a
group of operations or allow you to execute operations in an
:website:`ACID transaction </basics/acid-transactions>`. MongoDB
guarantees that the data involved in your transaction operations remains
consistent, even if the operations encounter unexpected errors.
:website:`ACID transaction </basics/acid-transactions>`.

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

The following example shows how to create a session by calling the ``StartSession()``
method:

.. code-block::

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: add the language here; also applies to the other code block

Suggested change
.. code-block::
.. code-block::
:language: csharp

var client = new MongoClient("mongodb://localhost:27017");
var session = client.StartSession();

.. warning::

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

ClientSessionOptions
~~~~~~~~~~~~~~~~~~~~

You can customize the behavior of your session by passing an instance of the
``ClientSessionOptions`` class to the ``StartSession()`` method. The following table
describes the properties that you can set on a ``ClientSessionOptions`` object:

.. list-table::
:widths: 20 80
:header-rows: 1

* - Property
- Description

* - ``CausalConsistency``
- Specifies whether the session is causally consistent. In a causally consistent session,
the driver executes operations in the order they were issued. To learn more, see
:ref:`<csharp-causal-consistency>`.
|
| **Data Type**: {+bool-data-type+}
| **Default**: ``true``

* - ``DefaultTransactionOptions``
- The default transaction options for the session. This includes the maximum commit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: I'd start with the verb to match the other descriptions

Suggested change
- The default transaction options for the session. This includes the maximum commit
- Specifies the default transaction options for the session. This includes the maximum commit

time, read concern, read preference, and write concern.
|
| **Data Type**: `TransactionOptions <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.TransactionOptions.html>`__
| **Default**: ``null``

* - ``Snapshot``
- Specifies whether the driver performs snapshot reads. To learn more about snapshot
reads, see :manual:`Read Concern "snapshot" </reference/read-concern-snapshot/>`
in the {+mdb-server+} manual.
|
| **Data Type**: {+bool-data-type+}
| **Default**: ``false``

The following code example shows how to create a session with custom options:

.. code-block::

var client = new MongoClient("mongodb://localhost:27017");
var sessionOptions = new ClientSessionOptions
{
CausalConsistency = true,
DefaultTransactionOptions = new TransactionOptions(
readConcern: ReadConcern.Available,
writeConcern: WriteConcern.Acknowledged)
};

var session = client.StartSession(sessionOptions);

.. _csharp-causal-consistency:

Causal Consistency
~~~~~~~~~~~~~~~~~~

Expand Down
Loading