Skip to content

Add AsyncTransaction #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2025
Merged

Conversation

geekgonecrazy
Copy link
Contributor

@geekgonecrazy geekgonecrazy commented Nov 7, 2024

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 geekgonecrazy marked this pull request as ready for review November 7, 2024 04:52
@graywolf336
Copy link

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

@jiangz222 jiangz222 merged commit 885a99b into qiniu:master Jul 5, 2025
2 of 3 checks passed
@geekgonecrazy geekgonecrazy deleted the asyncTransaction branch July 7, 2025 23:53
@geekgonecrazy
Copy link
Contributor Author

Thanks for accepting my change! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants