Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  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();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement