-
Notifications
You must be signed in to change notification settings - Fork 15
/
transaction.go
50 lines (39 loc) · 943 Bytes
/
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
package sqldblogger
import (
"context"
"database/sql/driver"
"time"
)
type transaction struct {
driver.Tx
id string
connID string
logger *logger
}
// Commit implement driver.Tx
func (tx *transaction) Commit() error {
lvl, start := LevelDebug, time.Now()
err := tx.Tx.Commit()
if err != nil {
lvl = LevelError
}
tx.logger.log(context.Background(), lvl, "Commit", start, err, tx.logData()...)
return err
}
// Rollback implement driver.Tx
func (tx *transaction) Rollback() error {
lvl, start := LevelDebug, time.Now()
err := tx.Tx.Rollback()
if err != nil {
lvl = LevelError
}
tx.logger.log(context.Background(), lvl, "Rollback", start, err, tx.logData()...)
return err
}
// logData default log data for transaction.
func (tx *transaction) logData() []dataFunc {
return []dataFunc{
tx.logger.withUID(tx.logger.opt.connIDFieldname, tx.connID),
tx.logger.withUID(tx.logger.opt.txIDFieldname, tx.id),
}
}