[JAVA-2866] Provide Transactions example for Docs Created: 23/May/18  Updated: 28/Oct/23  Resolved: 20/Jun/18

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Jeffrey Yemin
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
Related
related to JAVA-3219 Move transactions examples into main ... Backlog
Epic Link: Java 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



 Comments   
Comment by Jeffrey Yemin [ 27/Feb/19 ]

No, but probably should be. Can put it in 4.0 code base.

ian.whalen

Comment by Ian Whalen (Inactive) [ 28/Jan/19 ]

jeff.yemin is there a follow-on ticket to track moving this code into mongo-java-driver?

Comment by Jeffrey Yemin [ 20/Jun/18 ]

Closing this as docs have been published, though it's not ideal that the source is in a gist rather than the mongo-java-driver repository. I did it that way because to make the source look decent I needed to use Java 7/8 language features, and those are disabled in our build system.

Comment by Kay Kim (Inactive) [ 20/Jun/18 ]

Thanks much. Published https://docs.mongodb.com/master/core/transactions/#transactions-and-mongodb-drivers

Generated at Thu Feb 08 08:58:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.