Expose SessionContext of transactions

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      To use transaction the driver requires to create an 'inline' function for the SessionContext, while this is fine for many cases it doesn't work for all use cases.
       

      	if session, err = client.StartSession(); err != nil {
      		t.Fatal(err)
      	}
      	if err = session.StartTransaction(); err != nil {
      		t.Fatal(err)
      	}
      	if err = mongo.WithSession(ctx, session, func(sc mongo.SessionContext) error {
      		if result, err = collection.UpdateOne(sc, bson.M{"_id": id}, update); err != nil {
      			t.Fatal(err)
      		}
      		if result.MatchedCount != 1 || result.ModifiedCount != 1 {
      			t.Fatal("replace failed, expected 1 but got", result.MatchedCount)
      		}
      
      		if err = session.CommitTransaction(sc); err != nil {
      			t.Fatal(err)
      		}
      		return nil
      	}); err != nil {
      		t.Fatal(err)
      	}
      	session.EndSession(ctx)
      

      In our use case, we try to run all unit tests within their own transaction so that they can be rolled back and run independently of other tests running at the same time (as go tests run in parallel).

      For this, we run the unit tests using the testify suite where the transaction is started in the SetupTest and aborted in the TearDownTest. To avoid many duplicate lines of transaction-related code in the actual tests.

            Assignee:
            Unassigned
            Reporter:
            Paul van Brouwershaven
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: