Skip to content

Commit 2c17437

Browse files
authored
RUST-1637 Simplify convenient transaction example (#864)
1 parent 82d8473 commit 2c17437

File tree

1 file changed

+28
-46
lines changed
  • src/test/documentation_examples

1 file changed

+28
-46
lines changed

src/test/documentation_examples/mod.rs

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,9 +1830,8 @@ async fn change_streams_examples() -> Result<()> {
18301830
}
18311831

18321832
async fn convenient_transaction_examples() -> Result<()> {
1833-
use crate::{db::options::DatabaseOptions, options::WriteConcern};
1833+
use crate::ClientSession;
18341834
use futures::FutureExt;
1835-
use std::time::Duration;
18361835

18371836
let setup_client = Client::test_builder().build().await;
18381837
if !setup_client.supports_transactions() {
@@ -1851,67 +1850,50 @@ async fn convenient_transaction_examples() -> Result<()> {
18511850
// let uri = "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/";
18521851

18531852
let client = Client::with_uri_str(uri).await?;
1854-
let mut my_write_concern_majority = WriteConcern::MAJORITY;
1855-
my_write_concern_majority.w_timeout = Some(Duration::from_millis(1000));
18561853

18571854
// Prereq: Create collections. CRUD operations in transactions must be on existing collections.
18581855

18591856
client
1860-
.database_with_options(
1861-
"mydb1",
1862-
DatabaseOptions::builder()
1863-
.write_concern(my_write_concern_majority.clone())
1864-
.build(),
1865-
)
1857+
.database("mydb1")
18661858
.collection::<Document>("foo")
18671859
.insert_one(doc! { "abc": 0}, None)
18681860
.await?;
18691861
client
1870-
.database_with_options(
1871-
"mydb2",
1872-
DatabaseOptions::builder()
1873-
.write_concern(my_write_concern_majority.clone())
1874-
.build(),
1875-
)
1862+
.database("mydb2")
18761863
.collection::<Document>("bar")
18771864
.insert_one(doc! { "xyz": 0}, None)
18781865
.await?;
18791866

1880-
// Step 1: Start a client session.
1867+
// Step 1: Define the callback that specifies the sequence of operations to perform inside the
1868+
// transaction.
1869+
async fn callback(session: &mut ClientSession) -> Result<()> {
1870+
let collection_one = session
1871+
.client()
1872+
.database("mydb1")
1873+
.collection::<Document>("foo");
1874+
let collection_two = session
1875+
.client()
1876+
.database("mydb2")
1877+
.collection::<Document>("bar");
1878+
1879+
// Important: You must pass the session to the operations.
1880+
collection_one
1881+
.insert_one_with_session(doc! { "abc": 1 }, None, session)
1882+
.await?;
1883+
collection_two
1884+
.insert_one_with_session(doc! { "xyz": 999 }, None, session)
1885+
.await?;
1886+
1887+
Ok(())
1888+
}
18811889

1890+
// Step 2: Start a client session.
18821891
let mut session = client.start_session(None).await?;
18831892

1884-
// Step 2: Use with_transaction to start a transaction, execute the callback, and commit (or
1893+
// Step 3: Use with_transaction to start a transaction, execute the callback, and commit (or
18851894
// abort on error).
1886-
18871895
session
1888-
.with_transaction(
1889-
(),
1890-
|session, _| {
1891-
async move {
1892-
let collection_one = session
1893-
.client()
1894-
.database("mydb1")
1895-
.collection::<Document>("foo");
1896-
let collection_two = session
1897-
.client()
1898-
.database("mydb2")
1899-
.collection::<Document>("bar");
1900-
1901-
// Important:: You must pass the session to the operations.
1902-
collection_one
1903-
.insert_one_with_session(doc! { "abc": 1 }, None, session)
1904-
.await?;
1905-
collection_two
1906-
.insert_one_with_session(doc! { "xyz": 999 }, None, session)
1907-
.await?;
1908-
1909-
Ok(())
1910-
}
1911-
.boxed()
1912-
},
1913-
None,
1914-
)
1896+
.with_transaction((), |session, _| callback(session).boxed(), None)
19151897
.await?;
19161898

19171899
// End Transactions withTxn API Example 1

0 commit comments

Comments
 (0)