[CDRIVER-2663] Provide Transactions example for Docs Created: 23/May/18  Updated: 28/Oct/23  Resolved: 28/Jun/18

Status: Closed
Project: C Driver
Component/s: docs, libmongoc
Affects Version/s: None
Fix Version/s: 1.11.0, 1.12.0

Type: Improvement Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: A. Jesse Jiryu Davis
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by DRIVERS-488 Provide Transactions example for Docs Closed
Epic Link: CDRIVER MongoDB 4.0 Support

 Description   

Hi –
for the manual – would like to pop in driver examples in
https://docs.mongodb.com/master/core/transactions/#transactions-and-mongodb-drivers

We'll have the following example for the mongo shell. If we could get the corresponding driver example

Note: I've modified in the manual for the shell examples to have the transaction happen in a function updateEmployeeInfo(session) so as not to have that horrible txnAborted flag around – but whatever is good practice for the language of your choice. Let me know if I should update the example here to reflect that.

// Start Transaction Example 1
txnAborted = false;
 
// Start a session.
session = db.getMongo().startSession( { mode: "primary" });
 
employeesCollection = session.getDatabase("hr").employees;
eventsCollection = session.getDatabase("reporting").events;
 
// Start a transaction for the session that uses:
// - read concern "snapshot"
// - write concern "majority"
session.startTransaction( {
   readConcern: { level: "snapshot" },
   writeConcern: { w: "majority" }
} );
 
try{
 
   employeesCollection.updateOne( { employee: 3 }, { $set: { status: "Inactive" } } );
   eventsCollection.insertOne( { employee: 3, status: { new: "Inactive", old: "Active" } } );
 
} catch (error) {
   txnAborted = true;
   print ("Abort transaction.");
   session.abortTransaction();  // Uses write concern "majority"
}
 
// Until commit, no data changes from the two operations in the transaction are visible outside the transaction
 
if (!txnAborted) {
   print ("Commit transaction.");
   session.commitTransaction(); // Uses write concern "majority"
}
 
session.endSession();
 
// End Transaction Example 1

cc: rathi.gnanasekaran scott.lhommedieu behackett



 Comments   
Comment by Githook User [ 11/Feb/19 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: CDRIVER-2663 add location comments
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/8f087ff6509a91cc0a52e3146d82f250bcd5da3b

Comment by Githook User [ 28/Jun/18 ]

Author:

{'username': 'ajdavis', 'name': 'A. Jesse Jiryu Davis', 'email': 'jesse@mongodb.com'}

Message: CDRIVER-2663 update transaction examples
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/81bc391b4d3b9201e79757a28a055ace62405e42

Comment by Githook User [ 21/Jun/18 ]

Author:

{'username': 'ajdavis', 'name': 'A. Jesse Jiryu Davis', 'email': 'jesse@mongodb.com'}

Message: CDRIVER-2663 txn example for MongoDB Manual
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/01e2a84b28cde26c7bf720f6298053b76ad253af

Generated at Wed Feb 07 21:15:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.