-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
72 lines (59 loc) · 1.3 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// author: wsfuyibing <[email protected]>
// date: 2022-10-24
package db
import (
"context"
"fmt"
"xorm.io/xorm"
"github.com/fuyibing/log/v5"
)
type (
// TransactionHandler
// 事务Handler回调.
TransactionHandler func(ctx context.Context, sess *xorm.Session) error
)
// Transaction
// 创建事务.
func Transaction(ctx context.Context, handlers ...TransactionHandler) error {
sess := Connector.GetMasterWithContext(ctx)
defer func() {
_ = sess.Close()
}()
return TransactionWithSession(ctx, sess, handlers...)
}
// TransactionWithSession
// 基于指定连接创建事务.
func TransactionWithSession(ctx context.Context, sess *xorm.Session, handlers ...TransactionHandler) (err error) {
// 开启事务.
if err = sess.Begin(); err != nil {
return
}
// 事务完成.
defer func() {
// 捕获异常.
if r := recover(); r != nil {
if spa, exists := log.Span(ctx); exists {
spa.Logger().Fatal("transaction fatal: %v", r)
} else {
log.Fatal("transaction fatal: %v", r)
}
// 覆盖错误.
if err == nil {
err = fmt.Errorf("%v", err)
}
}
// 事务提交.
if err == nil {
_ = sess.Commit()
} else {
_ = sess.Rollback()
}
}()
// 遍历事务回调.
for _, handler := range handlers {
if err = handler(ctx, sess); err != nil {
break
}
}
return
}