@@ -20,24 +20,22 @@ Transactions
20
20
Overview
21
21
--------
22
22
23
- In this guide, you can learn how to use the {+driver-long+} to perform
24
- **transactions**. :manual:`Transactions </core/transactions/>` allow
25
- you to run a series of operations that do not change any data until the
26
- transaction is committed. If any operation in the transaction returns an
27
- error, the driver cancels the transaction and discards all data changes
28
- before they ever become visible.
23
+ In this guide, you can learn how to use the {+driver-short+} driver to perform
24
+ **transactions**. Transactions allowyou to run a series of operations that do
25
+ not change any data until the transaction is committed. If any operation in
26
+ the transaction returns an error, the driver cancels the transaction and discards
27
+ all data changes before they ever become visible.
29
28
30
29
In MongoDB, transactions run within logical **sessions**. A
31
- :manual:`session </reference/server-sessions/>` is a grouping of related
32
- 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
35
- 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.
39
-
40
- When using the {+driver-short+}, you can create a new session from a
30
+ session is a grouping of related read or write operations that you intend to run
31
+ sequentially. Sessions enable **causal consistency** for a
32
+ group of operations and allow you to run operations in an
33
+ **ACID-compliant transaction**, which is a transaction that meets an expectation
34
+ of atomicity, consistency, isolation, and durability. MongoDB guarantees that the
35
+ data involved in your transaction operations remains consistent, even if the
36
+ operations encounter unexpected errors.
37
+
38
+ When using {+driver-short+}, you can create a new session from a
41
39
``MongoClient`` instance as a ``ClientSession`` type. We recommend that you reuse
42
40
your ``MongoClient`` for multiple sessions and transactions instead of
43
41
creating a new client each time.
@@ -61,7 +59,7 @@ Methods
61
59
-------
62
60
63
61
After you start a session by using the ``start_session()`` method, you can manage
64
- the session state by using the methods provided by the returned ``ClientSession``.
62
+ the session state by using the following methods provided by the returned ``ClientSession``:
65
63
66
64
.. list-table::
67
65
:widths: 25 75
@@ -96,10 +94,10 @@ the session state by using the methods provided by the returned ``ClientSession`
96
94
</reference/method/Session.commitTransaction/>` in the Server manual.
97
95
98
96
* - ``with_transaction()``
99
- - | Starts a transaction on this session and executes a callback once, then
97
+ - | Starts a transaction on this session and runs `` callback`` once, then
100
98
commits the transaction. In the event of an exception, this method may retry
101
99
the commit or the entire transaction, which may invoke the callback multiple
102
- times by a single call to ``with_transaction``.
100
+ times by a single call to ``with_transaction() ``.
103
101
|
104
102
| **Parameters**: ``callback``, ``read_concern``, ``write_concern``, ``read_preference``, ``max_commit_time_ms``
105
103
| **Return Type**: ``_T``
@@ -109,8 +107,8 @@ the session state by using the methods provided by the returned ``ClientSession`
109
107
Returns an error if there is no active session to end.
110
108
111
109
A ``ClientSession`` also has methods to retrieve session
112
- properties and modify mutable session properties. View the :ref:`API
113
- documentation <api-docs-transaction>` to learn more about these methods .
110
+ properties and modify mutable session properties. To learn more about these
111
+ methods, see the :ref:`API documentation <api-docs-transaction>`.
114
112
115
113
Example
116
114
-------
@@ -121,9 +119,9 @@ following steps:
121
119
122
120
1. Create a session from the client using the ``start_session()`` method.
123
121
#. Use the ``with_transaction()`` method to start a transaction.
124
- #. Insert multiple documents. The ``with_transaction()`` method executes the
125
- insert and commits the transaction. If any operation results in
126
- errors, ``with_transaction()`` handles canceling the transaction. This method
122
+ #. Insert multiple documents. The ``with_transaction()`` method runs the
123
+ insert operation and commits the transaction. If any operation results in
124
+ errors, ``with_transaction()`` cancels the transaction. This method
127
125
ensures that the session closes properly when the block exits.
128
126
#. Close the connection to the server with the ``client.close()`` method.
129
127
@@ -158,7 +156,7 @@ API Documentation
158
156
~~~~~~~~~~~~~~~~~
159
157
160
158
To learn more about any of the types or methods discussed in this
161
- guide, see the following API Documentation :
159
+ guide, see the following API documentation :
162
160
163
161
- `ClientSession <{+api-root+}pymongo/client_session.html#pymongo.client_session.ClientSession>`__
164
162
- `WriteConcern <{+api-root+}pymongo/write_concern.html#pymongo.write_concern.WriteConcern>`__
0 commit comments