SHARE
TWEET

Untitled

a guest Jul 18th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Start a session.
  2. session = db.getMongo().startSession( { readPreference: { mode: "primary" } } );
  3.  
  4. // session = db.getMongo().startSession()
  5.  
  6. employeesCollection = session.getDatabase("hr").employees;
  7. eventsCollection = session.getDatabase("reporting").events;
  8.  
  9. // Start a transaction
  10. session.startTransaction( { readConcern: { level: "snapshot" }, writeConcern: { w: "majority" } } );
  11.  
  12. // session.startTransaction()
  13.  
  14. // Operations inside the transaction
  15. try {
  16.    employeesCollection.updateOne( { employee: 3 }, { $set: { status: "Inactive" } } );
  17.    eventsCollection.insertOne( { employee: 3, status: { new: "Inactive", old: "Active" } } );
  18. } catch (error) {
  19.    // Abort transaction on error
  20.    session.abortTransaction();
  21.    throw error;
  22. }
  23.  
  24. // Commit the transaction using write concern set at transaction start
  25. session.commitTransaction();
  26.  
  27. session.endSession();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top