4
4
Transactions
5
5
============
6
6
7
- .. default-domain:: mongodb
8
-
9
7
.. contents:: On this page
10
8
:local:
11
9
:backlinks: none
@@ -15,38 +13,35 @@ Transactions
15
13
Overview
16
14
--------
17
15
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**.
26
22
27
23
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.
35
29
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>`.
37
32
38
33
.. note::
39
34
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.
42
37
43
38
For a detailed list of limitations, see the :manual:`Transactions and
44
39
Operations </core/transactions/#transactions-and-operations>` section in
45
- the server manual.
40
+ the Server manual.
46
41
47
42
In MongoDB, multi-document transactions run within a **client session**.
48
43
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
50
45
your client for multiple sessions and transactions instead of
51
46
instantiating a new client each time.
52
47
@@ -256,9 +251,26 @@ shows how you can perform the following:
256
251
:start-after: start placeOrder
257
252
:end-before: end placeOrder
258
253
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
260
255
you want to run on that session.
261
256
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
+
262
274
The code and comments in the ``catch`` block demonstrate how you can identify
263
275
the server transaction errors and where you can place your logic to handle
264
276
them. Make sure to include the ``MongoError`` type from the driver in your
0 commit comments