-
Notifications
You must be signed in to change notification settings - Fork 63
/
transaction.go
39 lines (30 loc) · 1.1 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package mgm
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
)
// TransactionFunc is a handler to manage a transaction.
type TransactionFunc func(session mongo.Session, sc mongo.SessionContext) error
// Transaction creates a transaction with the default client.
func Transaction(f TransactionFunc) error {
return TransactionWithClient(ctx(), client, f)
}
// TransactionWithCtx creates a transaction with the given context and the default client.
func TransactionWithCtx(ctx context.Context, f TransactionFunc) error {
return TransactionWithClient(ctx, client, f)
}
// TransactionWithClient creates a transaction with the given client.
func TransactionWithClient(ctx context.Context, client *mongo.Client, f TransactionFunc) error {
session, err := client.StartSession() //start session need to get options.
if err != nil {
return err
}
defer session.EndSession(ctx)
if err = session.StartTransaction(); err != nil { // startTransaction need to get options.
return err
}
wrapperFn := func(sc mongo.SessionContext) error {
return f(session, sc)
}
return mongo.WithSession(ctx, session, wrapperFn)
}