-
Notifications
You must be signed in to change notification settings - Fork 27
/
tx.go
60 lines (52 loc) · 1.16 KB
/
tx.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
package proxy
import (
"context"
"database/sql/driver"
)
// Tx adds hook points into "database/sql/driver".Tx.
type Tx struct {
Tx driver.Tx
Proxy *Proxy
Conn *Conn
ctx context.Context
}
// Commit commits the transaction.
// It will trigger PreCommit, Commit, PostCommit hooks.
func (tx *Tx) Commit() error {
var err error
var ctx interface{}
hooks := tx.Proxy.getHooks(tx.ctx)
if hooks != nil {
defer func() { hooks.postCommit(tx.ctx, ctx, tx, err) }()
if ctx, err = hooks.preCommit(tx.ctx, tx); err != nil {
return err
}
}
if err = tx.Tx.Commit(); err != nil {
return err
}
if hooks != nil {
return hooks.commit(tx.ctx, ctx, tx)
}
return nil
}
// Rollback rollbacks the transaction.
// It will trigger PreRollback, Rollback, PostRollback hooks.
func (tx *Tx) Rollback() error {
var err error
var ctx interface{}
hooks := tx.Proxy.getHooks(tx.ctx)
if hooks != nil {
defer func() { hooks.postRollback(tx.ctx, ctx, tx, err) }()
if ctx, err = hooks.preRollback(tx.ctx, tx); err != nil {
return err
}
}
if err = tx.Tx.Rollback(); err != nil {
return err
}
if hooks != nil {
return hooks.rollback(tx.ctx, ctx, tx)
}
return nil
}