-
Notifications
You must be signed in to change notification settings - Fork 179
RUST-813 Add documentation examples for transactions #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35d0a64
add transaction example to ClientSession doc
isabelatkinson 55b4e0e
RUST-813 Add documentation examples for transactions
isabelatkinson 6a6e8a1
refactor
isabelatkinson 6f3ac9c
add snapshot read concern
isabelatkinson 42607c4
cr feedback
isabelatkinson a97addf
refactor to eliminate closure
isabelatkinson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#![allow(dead_code)] | ||
#![cfg(not(feature = "sync"))] | ||
|
||
// START TRANSACTIONS EXAMPLE | ||
use mongodb::{ | ||
bson::{doc, Document}, | ||
error::{Result, TRANSIENT_TRANSACTION_ERROR, UNKNOWN_TRANSACTION_COMMIT_RESULT}, | ||
options::{Acknowledgment, ReadConcern, TransactionOptions, WriteConcern}, | ||
ClientSession, | ||
}; | ||
|
||
async fn update_employee_info(session: &mut ClientSession) -> Result<()> { | ||
let transaction_options = TransactionOptions::builder() | ||
.read_concern(ReadConcern::snapshot()) | ||
.write_concern(WriteConcern::builder().w(Acknowledgment::Majority).build()) | ||
.build(); | ||
session.start_transaction(transaction_options).await?; | ||
|
||
execute_transaction_with_retry(session).await | ||
} | ||
|
||
async fn execute_transaction_with_retry(session: &mut ClientSession) -> Result<()> { | ||
while let Err(err) = execute_employee_info_transaction(session).await { | ||
println!("Transaction aborted. Error returned during transaction."); | ||
if err.contains_label(TRANSIENT_TRANSACTION_ERROR) { | ||
println!("Encountered TransientTransactionError, retrying transaction."); | ||
continue; | ||
} else { | ||
return Err(err); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
async fn execute_employee_info_transaction(session: &mut ClientSession) -> Result<()> { | ||
let client = session.client(); | ||
let employees = client.database("hr").collection::<Document>("employees"); | ||
let events = client | ||
.database("reporting") | ||
.collection::<Document>("events"); | ||
|
||
employees | ||
.update_one_with_session( | ||
doc! { "employee": 3 }, | ||
doc! { "$set": { "status": "Inactive" } }, | ||
None, | ||
session, | ||
) | ||
.await?; | ||
events | ||
.insert_one_with_session( | ||
doc! { "employee": 3, "status": { "new": "Inactive", "old": "Active" } }, | ||
None, | ||
session, | ||
) | ||
.await?; | ||
|
||
commit_with_retry(session).await | ||
} | ||
|
||
async fn commit_with_retry(session: &mut ClientSession) -> Result<()> { | ||
while let Err(err) = session.commit_transaction().await { | ||
if err.contains_label(UNKNOWN_TRANSACTION_COMMIT_RESULT) { | ||
println!("Encountered UnknownTransactionCommitResult, retrying commit operation."); | ||
continue; | ||
} else { | ||
println!("Encountered non-retryable error during commit."); | ||
return Err(err); | ||
} | ||
} | ||
println!("Transaction committed."); | ||
Ok(()) | ||
} | ||
// END TRANSACTIONS EXAMPLE |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.