@@ -1829,6 +1829,96 @@ async fn change_streams_examples() -> Result<()> {
18291829 Ok ( ( ) )
18301830}
18311831
1832+ async fn convenient_transaction_examples ( ) -> Result < ( ) > {
1833+ use crate :: { db:: options:: DatabaseOptions , options:: WriteConcern } ;
1834+ use futures:: FutureExt ;
1835+ use std:: time:: Duration ;
1836+
1837+ let setup_client = Client :: test_builder ( ) . build ( ) . await ;
1838+ if !setup_client. supports_transactions ( ) {
1839+ log_uncaptured (
1840+ "skipping convenient transaction API examples due to no transaction support" ,
1841+ ) ;
1842+ return Ok ( ( ) ) ;
1843+ }
1844+
1845+ let uri = DEFAULT_URI . clone ( ) ;
1846+ // Start Transactions withTxn API Example 1
1847+
1848+ // For a replica set, include the replica set name and a seedlist of the members in the URI
1849+ // string; e.g. let uri = "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?
1850+ // replicaSet=myRepl"; For a sharded cluster, connect to the mongos instances; e.g.
1851+ // let uri = "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/";
1852+
1853+ 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+
1857+ // Prereq: Create collections. CRUD operations in transactions must be on existing collections.
1858+
1859+ client
1860+ . database_with_options (
1861+ "mydb1" ,
1862+ DatabaseOptions :: builder ( )
1863+ . write_concern ( my_write_concern_majority. clone ( ) )
1864+ . build ( ) ,
1865+ )
1866+ . collection :: < Document > ( "foo" )
1867+ . insert_one ( doc ! { "abc" : 0 } , None )
1868+ . await ?;
1869+ client
1870+ . database_with_options (
1871+ "mydb2" ,
1872+ DatabaseOptions :: builder ( )
1873+ . write_concern ( my_write_concern_majority. clone ( ) )
1874+ . build ( ) ,
1875+ )
1876+ . collection :: < Document > ( "bar" )
1877+ . insert_one ( doc ! { "xyz" : 0 } , None )
1878+ . await ?;
1879+
1880+ // Step 1: Start a client session.
1881+
1882+ let mut session = client. start_session ( None ) . await ?;
1883+
1884+ // Step 2: Use with_transaction to start a transaction, execute the callback, and commit (or
1885+ // abort on error).
1886+
1887+ 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+ )
1915+ . await ?;
1916+
1917+ // End Transactions withTxn API Example 1
1918+
1919+ Ok ( ( ) )
1920+ }
1921+
18321922#[ cfg_attr( feature = "tokio-runtime" , tokio:: test) ]
18331923#[ cfg_attr( feature = "async-std-runtime" , async_std:: test) ]
18341924async fn test ( ) {
@@ -1855,4 +1945,5 @@ async fn test() {
18551945 run_command_examples ( ) . await . unwrap ( ) ;
18561946 index_examples ( ) . await . unwrap ( ) ;
18571947 change_streams_examples ( ) . await . unwrap ( ) ;
1948+ convenient_transaction_examples ( ) . await . unwrap ( ) ;
18581949}
0 commit comments