-
Notifications
You must be signed in to change notification settings - Fork 5
/
transaction.go
108 lines (93 loc) · 1.99 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package goerd
import (
"context"
"fmt"
"log"
"runtime/debug"
"github.com/jmoiron/sqlx"
)
func WithTx(ctx context.Context, f func(context.Context) error) error {
if HasTx(ctx) {
return f(ctx)
}
ctxTx, err := withTx(ctx)
if err != nil {
return err
}
commit := false
defer func() {
if r := recover(); r != nil || !commit {
if r != nil {
log.Printf("!!! TRANSACTION PANIC !!! : %s\n%s", r, string(debug.Stack()))
}
if e := Rollback(ctxTx); e != nil {
err = e
} else if r != nil {
err = fmt.Errorf("transaction panic: %s", r)
}
} else if commit {
if e := Commit(ctxTx); e != nil {
err = e
}
}
}()
if e := f(ctxTx); e != nil {
return e
}
commit = true
return nil
}
type CtxSqlxDb struct{}
func WithSqlxDb(ctx context.Context, d *sqlx.DB) context.Context {
return context.WithValue(ctx, CtxSqlxDb{}, d)
}
func SqlxDbFromContext(ctx context.Context) *sqlx.DB {
u, ok := ctx.Value(CtxSqlxDb{}).(*sqlx.DB)
if u == nil || !ok {
return nil
}
return u
}
type CtxSqlxTx struct{}
func WithSqlxTx(ctx context.Context, tx *sqlx.Tx) context.Context {
return context.WithValue(ctx, CtxSqlxTx{}, tx)
}
func SqlxTxFromContext(ctx context.Context) *sqlx.Tx {
u, ok := ctx.Value(CtxSqlxTx{}).(*sqlx.Tx)
if u == nil || !ok {
return nil
}
return u
}
func withTx(ctx context.Context) (context.Context, error) {
tx := SqlxTxFromContext(ctx)
if tx != nil {
return ctx, nil
}
d := SqlxDbFromContext(ctx)
if d == nil {
panic("sqlx DB not found in context")
}
tx, err := d.BeginTxx(ctx, nil)
if err != nil {
return ctx, err
}
return WithSqlxTx(ctx, tx), nil
}
func HasTx(ctx context.Context) bool {
return SqlxTxFromContext(ctx) != nil
}
func Commit(ctx context.Context) error {
tx := SqlxTxFromContext(ctx)
if tx == nil {
panic("transaction not found in context")
}
return tx.Commit()
}
func Rollback(ctx context.Context) error {
tx := SqlxTxFromContext(ctx)
if tx == nil {
panic("nil transaction not found in context")
}
return tx.Rollback()
}