@@ -1830,9 +1830,8 @@ async fn change_streams_examples() -> Result<()> {
1830
1830
}
1831
1831
1832
1832
async fn convenient_transaction_examples ( ) -> Result < ( ) > {
1833
- use crate :: { db :: options :: DatabaseOptions , options :: WriteConcern } ;
1833
+ use crate :: ClientSession ;
1834
1834
use futures:: FutureExt ;
1835
- use std:: time:: Duration ;
1836
1835
1837
1836
let setup_client = Client :: test_builder ( ) . build ( ) . await ;
1838
1837
if !setup_client. supports_transactions ( ) {
@@ -1851,67 +1850,50 @@ async fn convenient_transaction_examples() -> Result<()> {
1851
1850
// let uri = "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/";
1852
1851
1853
1852
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 ) ) ;
1856
1853
1857
1854
// Prereq: Create collections. CRUD operations in transactions must be on existing collections.
1858
1855
1859
1856
client
1860
- . database_with_options (
1861
- "mydb1" ,
1862
- DatabaseOptions :: builder ( )
1863
- . write_concern ( my_write_concern_majority. clone ( ) )
1864
- . build ( ) ,
1865
- )
1857
+ . database ( "mydb1" )
1866
1858
. collection :: < Document > ( "foo" )
1867
1859
. insert_one ( doc ! { "abc" : 0 } , None )
1868
1860
. await ?;
1869
1861
client
1870
- . database_with_options (
1871
- "mydb2" ,
1872
- DatabaseOptions :: builder ( )
1873
- . write_concern ( my_write_concern_majority. clone ( ) )
1874
- . build ( ) ,
1875
- )
1862
+ . database ( "mydb2" )
1876
1863
. collection :: < Document > ( "bar" )
1877
1864
. insert_one ( doc ! { "xyz" : 0 } , None )
1878
1865
. await ?;
1879
1866
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
+ }
1881
1889
1890
+ // Step 2: Start a client session.
1882
1891
let mut session = client. start_session ( None ) . await ?;
1883
1892
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
1885
1894
// abort on error).
1886
-
1887
1895
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 )
1915
1897
. await ?;
1916
1898
1917
1899
// End Transactions withTxn API Example 1
0 commit comments