/
tx.go
198 lines (169 loc) · 4.12 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package runner
import (
"errors"
"log"
"sync"
"time"
"github.com/jmoiron/sqlx"
"gopkg.in/mgutz/dat.v1"
)
const (
txPending = iota
txCommitted
txRollbacked
txErred
)
// ErrTxRollbacked occurs when Commit() or Rollback() is called on a
// transaction that has already been rollbacked.
var ErrTxRollbacked = errors.New("Nested transaction already rollbacked")
// Tx is a transaction for the given Session
type Tx struct {
sync.Mutex
*sqlx.Tx
*Queryable
IsRollbacked bool
state int
stateStack []int
// groupID is a unique ID used to log a group of queries
// within a transaction
groupID int64
}
// WrapSqlxTx creates a Tx from a sqlx.Tx
func WrapSqlxTx(tx *sqlx.Tx) *Tx {
newtx := &Tx{Tx: tx, Queryable: &Queryable{tx}}
if dat.Strict {
time.AfterFunc(1*time.Minute, func() {
if !newtx.IsRollbacked && newtx.state == txPending {
panic("A database transaction was not closed!")
}
})
}
return newtx
}
// Begin creates a transaction for the given database
func (db *DB) Begin() (*Tx, error) {
tx, err := db.DB.Beginx()
if err != nil {
return nil, logger.Error("begin.error", err)
}
logger.Debug("begin tx")
return WrapSqlxTx(tx), nil
}
// Begin returns this transaction
func (tx *Tx) Begin() (*Tx, error) {
tx.Lock()
defer tx.Unlock()
if tx.IsRollbacked {
return nil, ErrTxRollbacked
}
logger.Debug("begin nested tx")
tx.pushState()
return tx, nil
}
// Commit commits the transaction
func (tx *Tx) Commit() error {
tx.Lock()
defer tx.Unlock()
if tx.IsRollbacked {
return logger.Error("Cannot commit", ErrTxRollbacked)
}
if tx.state == txCommitted {
return logger.Error("Transaction has already been commited")
}
if tx.state == txRollbacked {
return logger.Error("Transaction has already been rollbacked")
}
if len(tx.stateStack) == 0 {
err := tx.Tx.Commit()
if err != nil {
tx.state = txErred
return logger.Error("commit.error", err)
}
}
logger.Debug("commit")
tx.state = txCommitted
return nil
}
// Rollback cancels the transaction
func (tx *Tx) Rollback() error {
tx.Lock()
defer tx.Unlock()
if tx.IsRollbacked {
return logger.Error("Cannot rollback", ErrTxRollbacked)
}
if tx.state == txCommitted {
return logger.Error("Cannot rollback, transaction has already been commited")
}
// rollback is sent to the database even in nested state
err := tx.Tx.Rollback()
if err != nil {
tx.state = txErred
return logger.Error("Unable to rollback", "err", err)
}
logger.Debug("rollback")
tx.state = txRollbacked
tx.IsRollbacked = true
return nil
}
// AutoCommit commits a transaction IF neither Commit or Rollback were called.
func (tx *Tx) AutoCommit() error {
tx.Lock()
defer tx.Unlock()
if tx.state == txRollbacked || tx.IsRollbacked {
tx.popState()
return nil
}
err := tx.Tx.Commit()
if err != nil {
tx.state = txErred
if dat.Strict {
log.Fatalf("Could not commit transaction: %s\n", err.Error())
}
tx.popState()
return logger.Error("transaction.AutoCommit.commit_error", err)
}
logger.Debug("autocommit")
tx.state = txCommitted
tx.popState()
return err
}
// AutoRollback rolls back transaction IF neither Commit or Rollback were called.
func (tx *Tx) AutoRollback() error {
tx.Lock()
defer tx.Unlock()
if tx.IsRollbacked || tx.state == txCommitted {
tx.popState()
return nil
}
err := tx.Tx.Rollback()
if err != nil {
tx.state = txErred
if dat.Strict {
log.Fatalf("Could not rollback transaction: %s\n", err.Error())
}
tx.popState()
return logger.Error("transaction.AutoRollback.rollback_error", err)
}
logger.Debug("autorollback")
tx.state = txRollbacked
tx.IsRollbacked = true
tx.popState()
return err
}
// Select creates a new SelectBuilder for the given columns.
// This disambiguates between Queryable.Select and sqlx's Select
func (tx *Tx) Select(columns ...string) *dat.SelectBuilder {
return tx.Queryable.Select(columns...)
}
func (tx *Tx) pushState() {
tx.stateStack = append(tx.stateStack, tx.state)
tx.state = txPending
}
func (tx *Tx) popState() {
if len(tx.stateStack) == 0 {
return
}
var val int
val, tx.stateStack = tx.stateStack[len(tx.stateStack)-1], tx.stateStack[:len(tx.stateStack)-1]
tx.state = val
}