Skip to content

Commit c907a4a

Browse files
authored
DOCSP-31847: use a session with the original client (#749)
* DOCSP-31847: use a session with the original client * small fix * MW PR fixes 1
1 parent eebc25c commit c907a4a

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

source/fundamentals/transactions.txt

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Transactions
55
============
66

7-
.. default-domain:: mongodb
8-
97
.. contents:: On this page
108
:local:
119
:backlinks: none
@@ -15,38 +13,35 @@ Transactions
1513
Overview
1614
--------
1715

18-
Read this guide to learn how to perform **transactions** in MongoDB using the
19-
Node.js driver. A transaction is a unit of work, composed of a series of
20-
operations that you want either to succeed together, or fail together when one
21-
or more of the operations fail. This behavior is called **atomicity**.
22-
Atomicity is a property in which transactions composed of one or more
23-
operations occur all at once, such that no other client can observe them as
24-
separate operations, and that it leaves no changes if one of the operations
25-
fails.
16+
In this guide, you can learn how to use the
17+
{+driver-short+} to perform **transactions**. Transactions allow you
18+
to run a series of operations that do not change any data until the
19+
entire transaction is committed. If any operation in the transaction fails, the
20+
driver aborts the transaction and discards all data changes before they
21+
ever become visible. This feature is called **atomicity**.
2622

2723
Since all write operations on a single document in MongoDB are atomic, you
28-
may benefit most from transactions when you must make an atomic change that
29-
modifies multiple documents, which is called a multi-document transaction.
30-
Similar to write operations on a single document, multi-document transactions
31-
are **ACID compliant**, which means MongoDB guarantees the data involved
32-
in your transaction operations remains consistent, even if it encounters
33-
unexpected errors. Learn more from this MongoDB article on
34-
`ACID transactions <https://www.mongodb.com/basics/acid-transactions>`__.
24+
might want to use transactions to make an atomic change that
25+
modifies multiple documents. This situation would require a multi-document transaction.
26+
Multi-document transactions are **ACID compliant** because MongoDB
27+
guarantees that the data involved in your transaction operations remains
28+
consistent, even if the driver encounters unexpected errors.
3529

36-
You can use the driver to perform multi-document transactions.
30+
To learn more about ACID compliance and transactions, see our :website:`article on
31+
ACID transactions </basics/acid-transactions>`.
3732

3833
.. note::
3934

40-
To run multi-document transactions, you must use MongoDB version 4.0 or
41-
later.
35+
To execute a multi-document transactions, you must be connected to a
36+
deployment running MongoDB Server version 4.0 or later.
4237

4338
For a detailed list of limitations, see the :manual:`Transactions and
4439
Operations </core/transactions/#transactions-and-operations>` section in
45-
the server manual.
40+
the Server manual.
4641

4742
In MongoDB, multi-document transactions run within a **client session**.
4843
A client session is a grouping of related read or write operations that
49-
you want to ensure run sequentially. We recommend you reuse
44+
you want to execute sequentially. We recommend you reuse
5045
your client for multiple sessions and transactions instead of
5146
instantiating a new client each time.
5247

@@ -256,9 +251,26 @@ shows how you can perform the following:
256251
:start-after: start placeOrder
257252
:end-before: end placeOrder
258253

259-
Note that you must pass the session object to each CRUD operation that
254+
You must pass the session object to each CRUD operation that
260255
you want to run on that session.
261256

257+
.. important:: Use a Session with the Client That Started It
258+
259+
Starting in version 6.0 of the {+driver-short+}, the driver
260+
throws an error if you provide a session from one ``MongoClient``
261+
instance to a different client instance.
262+
263+
For example, the following code generates an
264+
``MongoInvalidArgumentError`` error because it creates
265+
a ``ClientSession`` instance from the ``client1`` client, but provides
266+
this session to the ``client2`` client for a write operation:
267+
268+
.. code-block:: js
269+
:emphasize-lines: 2
270+
271+
const session = client1.startSession();
272+
client2.db('myDB').collection('myColl').insertOne({ name: 'Jane Eyre' }, { session });
273+
262274
The code and comments in the ``catch`` block demonstrate how you can identify
263275
the server transaction errors and where you can place your logic to handle
264276
them. Make sure to include the ``MongoError`` type from the driver in your

source/upgrade.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Version 6.x Breaking Changes
9494
- The ``Binary.write()`` method no longer accepts a string to write to the binary
9595
BSON object.
9696
- The ClientEncryption API returns promises instead of callbacks.
97+
- If you start a session on a client, then pass that session to a
98+
different client, the driver throws an error when you
99+
perform any operations in the session.
97100

98101
.. _node-breaking-changes-v5.x:
99102

source/whats-new.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ What's New in 6.0
7676

7777
- The ClientEncryption API returns promises instead of callbacks.
7878

79+
- If you start a session on a client, then pass that session to a
80+
different client, the driver throws an error when you
81+
perform any operations in the session.
82+
7983
The {+driver-short+} v6.0 release includes the following features:
8084

8185
.. important:: Deprecation Notice

0 commit comments

Comments
 (0)