-
Notifications
You must be signed in to change notification settings - Fork 3
/
tx.go
347 lines (316 loc) · 8.57 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package sql
import (
"context"
"database/sql"
"fmt"
"os"
"strconv"
"time"
"github.com/abulo/ratel/v3/core/logger"
"github.com/abulo/ratel/v3/core/metric"
"github.com/abulo/ratel/v3/core/trace"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
)
var (
// ErrNotFound is an alias of sql.ErrNoRows
// ErrNotFound = sql.ErrNoRows
errCantNestTx = errors.New("cannot nest transactions")
errNoRawDBFromTx = errors.New("cannot get raw db from transaction")
)
type (
beginnable func(*sql.DB, *pool) (trans, error)
// trans interface {
// Session
// Commit() error
// Rollback() error
// }
txConn struct {
Session
}
// txSession struct {
// *sql.Tx
// }
trans interface {
Session
Commit() error
Rollback() error
}
txSession struct {
*sql.Tx
disableMetric bool // 关闭指标采集
disableTrace bool // 关闭链路追踪
disablePrepare bool // 关闭预处理
driverName string // 驱动
dbName string
addr string
}
)
func begin(db *sql.DB, pool *pool) (trans, error) {
tx, err := db.Begin()
if err != nil {
return nil, err
}
return txSession{
Tx: tx,
disableTrace: pool.DisableTrace,
disableMetric: pool.DisableMetric,
disablePrepare: pool.DisablePrepare,
driverName: pool.DriverName,
dbName: pool.DbName,
addr: pool.Addr,
}, nil
}
func (s txConn) RawDB() (*sql.DB, error) {
return nil, errNoRawDBFromTx
}
func (s txConn) Transact(_ func(Session) error) error {
return errCantNestTx
}
func (s txConn) TransactCtx(_ context.Context, _ func(context.Context, Session) error) error {
return errCantNestTx
}
// NewSessionFromTx returns a Session with the given sql.Tx.
// Use it with caution, it's provided for other ORM to interact with.
func NewSessionFromTx(tx *sql.Tx) Session {
return txSession{Tx: tx}
}
func transact(ctx context.Context, db *commonSqlConn, pool *pool, b beginnable,
fn func(context.Context, Session) error) (err error) {
conn, err := db.connProv()
if err != nil {
return err
}
return transactOnConn(ctx, conn, pool, b, fn)
}
func transactOnConn(ctx context.Context, conn *sql.DB, pool *pool, b beginnable,
fn func(context.Context, Session) error) (err error) {
var tx trans
tx, err = b(conn, pool)
if err != nil {
return err
}
defer func() {
if p := recover(); p != nil {
if e := tx.Rollback(); e != nil {
err = fmt.Errorf("recover from %#v, rollback failed: %w", p, e)
} else {
err = fmt.Errorf("recover from %#v", p)
}
} else if err != nil {
if e := tx.Rollback(); e != nil {
err = fmt.Errorf("transaction failed: %s, rollback failed: %w", err, e)
}
} else {
err = tx.Commit()
}
}()
return fn(ctx, tx)
}
// MultiInsert 批量插入
func (db txSession) MultiInsert(ctx context.Context, query string, args ...any) (int64, error) {
res, err := db.ExecCtx(ctx, query, args...)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// Replace 替换
func (db txSession) Replace(ctx context.Context, query string, args ...any) (int64, error) {
res, err := db.ExecCtx(ctx, query, args...)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// InsertUpdate 插入或更新
func (db txSession) InsertUpdate(ctx context.Context, query string, args ...any) (int64, error) {
res, err := db.ExecCtx(ctx, query, args...)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// Insert 插入
func (db txSession) Insert(ctx context.Context, query string, args ...any) (int64, error) {
res, err := db.ExecCtx(ctx, query, args...)
if err != nil {
return 0, err
}
return res.LastInsertId()
}
// Update 更新
func (db txSession) Update(ctx context.Context, query string, args ...any) (int64, error) {
res, err := db.ExecCtx(ctx, query, args...)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// Delete 删除
func (db txSession) Delete(ctx context.Context, query string, args ...any) (int64, error) {
res, err := db.ExecCtx(ctx, query, args...)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// Exec executes a query without returning any rows.
func (db txSession) Exec(ctx context.Context, query string, args ...any) (int64, error) {
res, err := db.ExecCtx(ctx, query, args...)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// QueryRow returns a single row from the database.
func (db txSession) QueryRow(ctx context.Context, query string, args ...any) *Row {
res, err := db.QueryCtx(ctx, query, args...)
if err != nil {
return &Row{err: err}
}
return &Row{
rows: &Rows{rows: res},
err: err,
}
}
func (db txSession) Count(ctx context.Context, query string, args ...any) (int64, error) {
res, err := db.QueryCtx(ctx, query, args...)
if err != nil {
return 0, err
}
result := &Row{
rows: &Rows{rows: res},
err: err,
}
d, err := result.ToMap()
if err != nil || d == nil {
return 0, err
}
if len(d) < 1 {
return 0, nil
}
v := d["_C"]
return strconv.ParseInt(v, 10, 0)
}
// QueryRow returns a single row from the database.
func (db txSession) QueryRows(ctx context.Context, query string, args ...any) *Rows {
res, err := db.QueryCtx(ctx, query, args...)
if err != nil {
return &Rows{err: err}
}
return &Rows{
rows: res,
err: err,
}
}
func (db txSession) ExecCtx(ctx context.Context, query string, args ...any) (result sql.Result, err error) {
ctx = getCtx(ctx)
start := time.Now()
conn := db.Tx
if !db.disableTrace {
call := Caller(11)
if parentSpan := trace.SpanFromContext(ctx); parentSpan != nil {
parentCtx := parentSpan.Context()
span := opentracing.StartSpan(db.driverName, opentracing.ChildOf(parentCtx))
ext.SpanKindRPCClient.Set(span)
hostName, err := os.Hostname()
if err != nil {
hostName = "unknown"
}
ext.PeerHostname.Set(span, hostName)
ext.PeerAddress.Set(span, db.addr)
ext.DBInstance.Set(span, db.dbName)
ext.DBStatement.Set(span, db.driverName)
sqlRaw, _ := Format(query, args...)
span.LogFields(log.String("sql", sqlRaw))
span.LogFields(log.Object("call", call))
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
}
}
if !db.disablePrepare {
var stmt *sql.Stmt
//添加预处理
stmt, errStmt := conn.PrepareContext(ctx, query)
if err != nil {
err = errStmt
return
}
defer func() {
if err := stmt.Close(); err != nil {
logger.Logger.Error("Error closing stmt: ", err)
}
}()
result, err = stmt.ExecContext(ctx, args...)
} else {
result, err = conn.ExecContext(ctx, query, args...)
}
if !db.disableMetric {
cost := time.Since(start)
if err != nil {
metric.LibHandleCounter.WithLabelValues(db.driverName, db.dbName, db.addr, "ERR").Inc()
} else {
metric.LibHandleCounter.Inc(db.driverName, db.dbName, db.addr, "OK")
}
metric.LibHandleHistogram.WithLabelValues(db.driverName, db.dbName, db.addr).Observe(cost.Seconds())
}
return
}
func (db txSession) QueryCtx(ctx context.Context, query string, args ...any) (result *sql.Rows, err error) {
ctx = getCtx(ctx)
start := time.Now()
conn := db.Tx
if !db.disableTrace {
call := Caller(11)
if parentSpan := trace.SpanFromContext(ctx); parentSpan != nil {
parentCtx := parentSpan.Context()
span := opentracing.StartSpan(db.driverName, opentracing.ChildOf(parentCtx))
ext.SpanKindRPCClient.Set(span)
hostName, err := os.Hostname()
if err != nil {
hostName = "unknown"
}
ext.PeerHostname.Set(span, hostName)
ext.PeerAddress.Set(span, db.addr)
ext.DBInstance.Set(span, db.dbName)
ext.DBStatement.Set(span, db.driverName)
sqlRaw, _ := Format(query, args...)
span.LogFields(log.String("sql", sqlRaw))
span.LogFields(log.Object("call", call))
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
}
}
if !db.disablePrepare {
var stmt *sql.Stmt
//添加预处理
stmt, errStmt := conn.PrepareContext(ctx, query)
if err != nil {
err = errStmt
return
}
defer func() {
if err := stmt.Close(); err != nil {
// call := Caller(12)
logger.Logger.Error("Error closing stmt: ", err)
// fmt.Println(Format(query, args...))
}
}()
result, err = stmt.QueryContext(ctx, args...)
} else {
result, err = conn.QueryContext(ctx, query, args...)
}
if !db.disableMetric {
cost := time.Since(start)
if err != nil {
metric.LibHandleCounter.WithLabelValues(db.driverName, db.dbName, db.addr, "ERR").Inc()
} else {
metric.LibHandleCounter.Inc(db.driverName, db.dbName, db.addr, "OK")
}
metric.LibHandleHistogram.WithLabelValues(db.driverName, db.dbName, db.addr).Observe(cost.Seconds())
}
return
}