@@ -526,23 +526,36 @@ func ExampleWithSession() {
526
526
}
527
527
defer sess .EndSession (context .TODO ())
528
528
529
- // Call WithSession to use the new Session to insert a document and find it .
529
+ // Call WithSession to start a transaction within the new session .
530
530
err = mongo .WithSession (context .TODO (), sess , func (sessCtx mongo.SessionContext ) error {
531
531
// Use sessCtx as the Context parameter for InsertOne and FindOne so both operations are run under the new
532
532
// Session.
533
533
534
+ if err := sess .StartTransaction (); err != nil {
535
+ return err
536
+ }
537
+
534
538
coll := client .Database ("db" ).Collection ("coll" )
535
539
res , err := coll .InsertOne (sessCtx , bson.D {{"x" , 1 }})
536
540
if err != nil {
541
+ // Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
542
+ // successfully even if the context passed to mongo.WithSession is changed to have a timeout.
543
+ _ = sess .AbortTransaction (context .Background ())
537
544
return err
538
545
}
539
546
540
547
var result bson.M
541
548
if err = coll .FindOne (sessCtx , bson.D {{"_id" , res .InsertedID }}).Decode (result ); err != nil {
549
+ // Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
550
+ // successfully even if the context passed to mongo.WithSession is changed to have a timeout.
551
+ _ = sess .AbortTransaction (context .Background ())
542
552
return err
543
553
}
544
554
fmt .Println (result )
545
- return nil
555
+
556
+ // Use context.Background() to ensure that the commit can complete successfully even if the context passed to
557
+ // mongo.WithSession is changed to have a timeout.
558
+ return sess .CommitTransaction (context .Background ())
546
559
})
547
560
}
548
561
@@ -558,18 +571,31 @@ func ExampleClient_UseSessionWithOptions() {
558
571
// Use sessCtx as the Context parameter for InsertOne and FindOne so both operations are run under the new
559
572
// Session.
560
573
574
+ if err := sessCtx .StartTransaction (); err != nil {
575
+ return err
576
+ }
577
+
561
578
coll := client .Database ("db" ).Collection ("coll" )
562
579
res , err := coll .InsertOne (sessCtx , bson.D {{"x" , 1 }})
563
580
if err != nil {
581
+ // Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
582
+ // successfully even if the context passed to mongo.WithSession is changed to have a timeout.
583
+ _ = sessCtx .AbortTransaction (context .Background ())
564
584
return err
565
585
}
566
586
567
587
var result bson.M
568
588
if err = coll .FindOne (sessCtx , bson.D {{"_id" , res .InsertedID }}).Decode (result ); err != nil {
589
+ // Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
590
+ // successfully even if the context passed to mongo.WithSession is changed to have a timeout.
591
+ _ = sessCtx .AbortTransaction (context .Background ())
569
592
return err
570
593
}
571
594
fmt .Println (result )
572
- return nil
595
+
596
+ // Use context.Background() to ensure that the commit can complete successfully even if the context passed to
597
+ // mongo.WithSession is changed to have a timeout.
598
+ return sessCtx .CommitTransaction (context .Background ())
573
599
})
574
600
if err != nil {
575
601
log .Fatal (err )
@@ -634,17 +660,24 @@ func ExampleNewSessionContext() {
634
660
coll := client .Database ("db" ).Collection ("coll" )
635
661
res , err := coll .InsertOne (sessCtx , bson.D {{"x" , 1 }})
636
662
if err != nil {
663
+ // Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
664
+ // successfully even if the context passed to NewSessionContext is changed to have a timeout.
665
+ _ = sess .AbortTransaction (context .Background ())
637
666
panic (err )
638
667
}
639
668
640
669
var result bson.M
641
670
if err = coll .FindOne (sessCtx , bson.D {{"_id" , res .InsertedID }}).Decode (& result ); err != nil {
671
+ // Abort the transaction after an error. Use context.Background() to ensure that the abort can complete
672
+ // successfully even if the context passed to NewSessionContext is changed to have a timeout.
673
+ _ = sess .AbortTransaction (context .Background ())
642
674
panic (err )
643
675
}
644
676
fmt .Printf ("result: %v\n " , result )
645
677
646
- // Commit the transaction so the inserted document will be stored.
647
- if err = sess .CommitTransaction (sessCtx ); err != nil {
678
+ // Commit the transaction so the inserted document will be stored. Use context.Background() to ensure that the
679
+ // commit can complete successfully even if the context passed to NewSessionContext is changed to have a timeout.
680
+ if err = sess .CommitTransaction (context .Background ()); err != nil {
648
681
panic (err )
649
682
}
650
683
}
0 commit comments