Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Start a session.
- session = db.getMongo().startSession( { readPreference: { mode: "primary" } } );
- // session = db.getMongo().startSession()
- employeesCollection = session.getDatabase("hr").employees;
- eventsCollection = session.getDatabase("reporting").events;
- // Start a transaction
- session.startTransaction( { readConcern: { level: "snapshot" }, writeConcern: { w: "majority" } } );
- // session.startTransaction()
- // Operations inside the transaction
- try {
- employeesCollection.updateOne( { employee: 3 }, { $set: { status: "Inactive" } } );
- eventsCollection.insertOne( { employee: 3, status: { new: "Inactive", old: "Active" } } );
- } catch (error) {
- // Abort transaction on error
- session.abortTransaction();
- throw error;
- }
- // Commit the transaction using write concern set at transaction start
- session.commitTransaction();
- session.endSession();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement