I am unable to make the transactions to work in python. Looked at the example scripts, read documentation. I know I am missing something. Appreciate your help.
The error is Database has no attribute ‘_retryable_write’. To access the _retryable_write collection, use database[’_retryable_write’].
Here is the code
try:
client = pymongo.MongoClient(db_string)
airbnb = client.get_database("sample_airbnb")
session = client.start_session()
session.start_transaction()
transaction_collection = pymongo.collection.Collection(client, "gmm_transaction", session=session)
result = transaction_collection.insert_one({
"transaction_id": "x",
"product_id": "y",
"cost": 12.34,
"split_percent": 0.23,
"designated_buyer_id": "Ram",
"designated_receiver_id": "Lakshman",
"buy_type": 1,
"reference": "xys",
"status": 1,
"created_dt": datetime.now().strftime("%d/%m/%Y %H:%M:%S")
}, session=session)
return airbnb
except errors.PyMongoError as e:
print("The error message is ", e)
return None
except Exception as e:
print("The generic error is", e)
return None
I have added “retryWrites=true” to the connection string as well.
Thanks for your help