2121
2222.. code-block:: python
2323
24- with client.start_session(causal_consistency=True) as session:
24+ async with client.start_session(causal_consistency=True) as session:
2525 collection = client.db.collection
2626 await collection.update_one({"_id": 1}, {"$set": {"x": 10}}, session=session)
2727 secondary_c = collection.with_options(read_preference=ReadPreference.SECONDARY)
5353
5454 orders = client.db.orders
5555 inventory = client.db.inventory
56- with client.start_session() as session:
57- async with session.start_transaction():
56+ async with client.start_session() as session:
57+ async with await session.start_transaction():
5858 await orders.insert_one({"sku": "abc123", "qty": 100}, session=session)
5959 await inventory.update_one(
6060 {"sku": "abc123", "qty": {"$gte": 100}},
6161 {"$inc": {"qty": -100}},
6262 session=session,
6363 )
6464
65- Upon normal completion of ``async with session.start_transaction()`` block, the
65+ Upon normal completion of ``async with await session.start_transaction()`` block, the
6666transaction automatically calls :meth:`AsyncClientSession.commit_transaction`.
6767If the block exits with an exception, the transaction automatically calls
6868:meth:`AsyncClientSession.abort_transaction`.
113113.. code-block:: python
114114
115115 # Each read using this session reads data from the same point in time.
116- with client.start_session(snapshot=True) as session:
116+ async with client.start_session(snapshot=True) as session:
117117 order = await orders.find_one({"sku": "abc123"}, session=session)
118118 inventory = await inventory.find_one({"sku": "abc123"}, session=session)
119119
@@ -619,7 +619,7 @@ async def callback(session):
619619 await inventory.update_one({"sku": "abc123", "qty": {"$gte": 100}},
620620 {"$inc": {"qty": -100}}, session=session)
621621
622- with client.start_session() as session:
622+ async with client.start_session() as session:
623623 await session.with_transaction(callback)
624624
625625 To pass arbitrary arguments to the ``callback``, wrap your callable
@@ -628,7 +628,7 @@ async def callback(session):
628628 async def callback(session, custom_arg, custom_kwarg=None):
629629 # Transaction operations...
630630
631- with client.start_session() as session:
631+ async with client.start_session() as session:
632632 await session.with_transaction(
633633 lambda s: callback(s, "custom_arg", custom_kwarg=1))
634634
0 commit comments