Skip to content

Commit 8376d6d

Browse files
author
Divjot Arora
committed
GODRIVER-1703 Update Sessions examples to use transactions (#478)
1 parent 1ea066b commit 8376d6d

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

mongo/crud_examples_test.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,23 +526,36 @@ func ExampleWithSession() {
526526
}
527527
defer sess.EndSession(context.TODO())
528528

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.
530530
err = mongo.WithSession(context.TODO(), sess, func(sessCtx mongo.SessionContext) error {
531531
// Use sessCtx as the Context parameter for InsertOne and FindOne so both operations are run under the new
532532
// Session.
533533

534+
if err := sess.StartTransaction(); err != nil {
535+
return err
536+
}
537+
534538
coll := client.Database("db").Collection("coll")
535539
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
536540
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())
537544
return err
538545
}
539546

540547
var result bson.M
541548
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())
542552
return err
543553
}
544554
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())
546559
})
547560
}
548561

@@ -558,18 +571,31 @@ func ExampleClient_UseSessionWithOptions() {
558571
// Use sessCtx as the Context parameter for InsertOne and FindOne so both operations are run under the new
559572
// Session.
560573

574+
if err := sessCtx.StartTransaction(); err != nil {
575+
return err
576+
}
577+
561578
coll := client.Database("db").Collection("coll")
562579
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
563580
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())
564584
return err
565585
}
566586

567587
var result bson.M
568588
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())
569592
return err
570593
}
571594
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())
573599
})
574600
if err != nil {
575601
log.Fatal(err)
@@ -634,17 +660,24 @@ func ExampleNewSessionContext() {
634660
coll := client.Database("db").Collection("coll")
635661
res, err := coll.InsertOne(sessCtx, bson.D{{"x", 1}})
636662
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())
637666
panic(err)
638667
}
639668

640669
var result bson.M
641670
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())
642674
panic(err)
643675
}
644676
fmt.Printf("result: %v\n", result)
645677

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 {
648681
panic(err)
649682
}
650683
}

0 commit comments

Comments
 (0)