Skip to content

Commit 283331a

Browse files
committed
implement feedback
1 parent ea860a8 commit 283331a

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

source/includes/write/transaction.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
# start-transaction
2-
# Establish a connection to the MongoDB server
2+
# Establishes a connection to the MongoDB server
33
client = MongoClient("<connection string>")
44

5-
# Define the database and collection
5+
# Defines the database and collection
66
restaurants_db = client["sample_restaurants"]
77
restaurants_collection = restaurants_db["restaurants"]
88

9-
# Function to perform the transaction
9+
# Function performs the transaction
1010
def insert_documents(session):
1111
restaurants_collection_with_session = restaurants_collection.with_options(
1212
write_concern=WriteConcern("majority"),
1313
read_concern=ReadConcern("local")
1414
)
1515

16-
# Insert documents within the transaction
16+
# Inserts documents within the transaction
1717
restaurants_collection_with_session.insert_one(
1818
{"name": "PyMongo Pizza", "cuisine": "Pizza"}, session=session
1919
)
2020
restaurants_collection_with_session.insert_one(
2121
{"name": "PyMongo Burger", "cuisine": "Burger"}, session=session
2222
)
2323

24-
# Start a client session
24+
# Starts a client session
2525
with client.start_session() as session:
2626
try:
27-
# Use the with_transaction method to start a transaction, execute the callback, and commit (or abort on error).
27+
# Uses the with_transaction method to start a transaction, execute the callback, and commit (or abort on error).
2828
session.with_transaction(insert_documents)
2929
print("Transaction succeeded")
3030
except (ConnectionFailure, OperationFailure) as e:
3131
print(f"Transaction failed: {e}")
3232

33-
# Close the client connection
33+
# Closes the client connection
3434
client.close()
3535
# end-transaction

source/write/transactions.txt

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,22 @@ Transactions
2020
Overview
2121
--------
2222

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.
2928

3029
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
4139
``MongoClient`` instance as a ``ClientSession`` type. We recommend that you reuse
4240
your ``MongoClient`` for multiple sessions and transactions instead of
4341
creating a new client each time.
@@ -61,7 +59,7 @@ Methods
6159
-------
6260

6361
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``:
6563

6664
.. list-table::
6765
:widths: 25 75
@@ -96,10 +94,10 @@ the session state by using the methods provided by the returned ``ClientSession`
9694
</reference/method/Session.commitTransaction/>` in the Server manual.
9795

9896
* - ``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
10098
commits the transaction. In the event of an exception, this method may retry
10199
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()``.
103101
|
104102
| **Parameters**: ``callback``, ``read_concern``, ``write_concern``, ``read_preference``, ``max_commit_time_ms``
105103
| **Return Type**: ``_T``
@@ -109,8 +107,8 @@ the session state by using the methods provided by the returned ``ClientSession`
109107
Returns an error if there is no active session to end.
110108

111109
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>`.
114112

115113
Example
116114
-------
@@ -121,9 +119,9 @@ following steps:
121119

122120
1. Create a session from the client using the ``start_session()`` method.
123121
#. 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
127125
ensures that the session closes properly when the block exits.
128126
#. Close the connection to the server with the ``client.close()`` method.
129127

@@ -158,7 +156,7 @@ API Documentation
158156
~~~~~~~~~~~~~~~~~
159157

160158
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:
162160

163161
- `ClientSession <{+api-root+}pymongo/client_session.html#pymongo.client_session.ClientSession>`__
164162
- `WriteConcern <{+api-root+}pymongo/write_concern.html#pymongo.write_concern.WriteConcern>`__

0 commit comments

Comments
 (0)