I don’t understand what going on,I have done exactly what the docs says.
In my app you would see i have two files, main
and database
, I defined dd()
in main
which I passed to do_transactions
.
The problems
- 1: The transaction is committed even when that line of code was never executed,
query
returnedNone
. - 2: The transaction is still committed when
await session.commit_transaction()
is removed or commented,query
returnedNone
.
when I comment/removed callback()
the transaction does not get committed, what could be the problem?
main.py
import asyncio
from motor.motor_asyncio import AsyncIOMotorDatabase
async def test():
from .database import do_transactions
async def dd(database: AsyncIOMotorDatabase) -> int:
comments_collection = database.get_collection("comments")
test_collection = database.get_collection("test")
await test_collection.delete_many({})
await comments_collection.insert_one({"name": "test"})
return 449
query = await do_transactions(dd, bool)
pprint(query)
if __name__ == "__main__":
asyncio.run(test())
database.py
from motor.motor_asyncio import (
AsyncIOMotorClient,
AsyncIOMotorCollection,
AsyncIOMotorDatabase,
AsyncIOMotorClientSession,
)
T = TypeVar("T")
async def do_transactions(
self,
callback: Callable[[AsyncIOMotorDatabase], Coroutine[Any, Any, T]],
return_type: Type[T],
) -> T | None:
client=AsyncIOMotorClient("uri")
async with await client.start_session() as session:
db = session.client.get_database(name="database")
try:
async with session.start_transaction():
result = 777 # this works fine
#result = await callback(db) transaction will be committed if this is uncommented
if not isinstance(result, return_type):
await session.abort_transaction()
return None
await session.commit_transaction() # removing this still commits the transaction if callback() is uncommented
await session.end_session()
return result
except Exception as e:
await session.abort_transaction()
return None