[GODRIVER-1523] Expose SessionContext of transactions Created: 06/Mar/20  Updated: 06/Mar/20  Resolved: 06/Mar/20

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Paul van Brouwershaven Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates GODRIVER-1104 Provide imperative transaction API Closed

 Description   

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.



 Comments   
Comment by Divjot Arora (Inactive) [ 06/Mar/20 ]

Hi paul@vanbrouwershaven.com,

I believe this is a duplicate of GODRIVER-1104, which is scheduled to be done during the 1.4.0 cycle. I'm closing this ticket for now, but feel free to leave a comment if I've misunderstood something or you have any other questions.

– Divjot

Generated at Thu Feb 08 08:36:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.