qmgo icon indicating copy to clipboard operation
qmgo copied to clipboard

Add AsyncTransaction

Open geekgonecrazy opened this issue 1 year ago • 1 comments

Doing a transaction in a callback makes writing go code very cumbersome and maybe a bit javascript like. (which the official mongo go sdk definitely has that feeling). They have a less visible / documented way using session context.

I added a StartAsyncTransaction method to the session that returns a new context that as long as passed in with insert/updates will be associated with the transaction.

Example usage:

	session, err := cli.Session()
	if err != nil {
		return err
	}

	sCtx, err := session.StartAsyncTransaction(ctx)
	if err != nil {
		return err
	}

	defer session.EndSession(sCtx)
        defer session.AbortAsyncTransaction(sCtx)

	if _, err := cli.InsertOne(sCtx, bson.D{{"abc", int32(1)}}); err != nil {
		return err
	}

	if err := session.CommitAsyncTransaction(sCtx); err != nil {
		return err
	}

geekgonecrazy avatar Nov 07 '24 04:11 geekgonecrazy

Any idea if or when this PR will be considered for being added?

graywolf336 avatar May 11 '25 22:05 graywolf336

Thanks for accepting my change! :pray:

geekgonecrazy avatar Jul 07 '25 23:07 geekgonecrazy