-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
107 lines (96 loc) · 2.95 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
package postgres
import (
"context"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/neuronlabs/neuron/errors"
"github.com/neuronlabs/neuron/query"
"github.com/neuronlabs/neuron/repository"
"github.com/neuronlabs/neuron-extensions/repository/postgres/log"
)
// compile time check for the repository.Transactioner interface.
var _ repository.Transactioner = &Postgres{}
// Begin starts a transaction for the given scope.
// Implements Begin method of the query.Transactioner interface.
func (p *Postgres) Begin(ctx context.Context, tx *query.Transaction) error {
if _, ok := p.checkTransaction(tx.ID); ok {
return nil
}
var isolation pgx.TxIsoLevel
txOpts := pgx.TxOptions{IsoLevel: isolation}
if tx.Options != nil {
switch tx.Options.Isolation {
case query.LevelDefault:
case query.LevelSerializable:
isolation = pgx.Serializable
case query.LevelReadCommitted:
isolation = pgx.ReadCommitted
case query.LevelReadUncommitted:
isolation = pgx.ReadUncommitted
case query.LevelRepeatableRead, query.LevelSnapshot:
isolation = pgx.RepeatableRead
default:
return errors.WrapDetf(query.ErrTxState, "unsupported isolation level: %s", tx.Options.Isolation.String())
}
txOpts.IsoLevel = isolation
if tx.Options.ReadOnly {
txOpts.AccessMode = pgx.ReadOnly
}
}
pgxTx, err := p.ConnPool.BeginTx(ctx, txOpts)
if err != nil {
return err
}
if log.Level().IsAllowed(log.LevelDebug3) {
log.Debug3f("[POSTGRES:%s][TX:%s] BEGIN;", p.id, tx.ID)
}
p.setTransaction(tx.ID, pgxTx)
return nil
}
// Commit commits the scope's transaction
// Implements Commit method from the query.Commiter interface
func (p *Postgres) Commit(ctx context.Context, tx *query.Transaction) error {
if tx == nil {
return errors.WrapDet(query.ErrTxInvalid, "scope's transaction is nil")
}
pgxTx, ok := p.checkTransaction(tx.ID)
if !ok {
log.Errorf("Transaction: '%s' no mapped SQL transaction found", tx.ID)
return errors.WrapDet(query.ErrTxInvalid, "no mapped sql transaction found for the scope")
}
defer p.clearTransaction(tx.ID)
for {
err := pgxTx.Commit(ctx)
if err == nil {
break
}
if pgconn.SafeToRetry(err) {
continue
}
return errors.WrapDetf(p.neuronError(err), "commit transaction: %s failed: %v", tx.ID, err)
}
return nil
}
// Rollback rolls back the transaction for given scope
func (p *Postgres) Rollback(ctx context.Context, tx *query.Transaction) error {
if tx == nil {
return errors.WrapDet(query.ErrTxInvalid, "scope's transaction is nil")
}
pgxTx, ok := p.checkTransaction(tx.ID)
if !ok {
log.Errorf("Transaction: '%s' no mapped SQL transaction found", tx.ID)
return errors.WrapDet(query.ErrTxInvalid, "no mapped sql transaction found for the scope")
}
defer p.clearTransaction(tx.ID)
for {
err := pgxTx.Rollback(ctx)
if err == nil {
break
}
if pgconn.SafeToRetry(err) {
continue
}
return errors.WrapDetf(p.neuronError(err), "rollback transaction: %s failed: %v", tx.ID, err)
}
return nil
}