-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtx.go
41 lines (34 loc) · 996 Bytes
/
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
package pig
import (
"github.com/jackc/pgx/v4"
"github.com/pkg/errors"
)
const (
transactionTimeoutQuery = "SET local idle_in_transaction_session_timeout = $1"
statementTimeoutQuery = "SET local statement_timeout = $1"
)
// Tx transaction.
type Tx struct {
conn Conn
options Options
}
// Exec to execute transaction.
func (tx *Tx) Exec(handler Handler) error {
err := tx.conn.BeginFunc(tx.options.Context, func(txx pgx.Tx) error {
if tx.options.TransactionTimeout > 0 {
if _, err := txx.Exec(tx.options.Context, transactionTimeoutQuery, tx.options.TransactionTimeout); err != nil {
return errors.Wrap(err, "pig: set transaction timeout")
}
}
if tx.options.StatementTimeout > 0 {
if _, err := txx.Exec(tx.options.Context, statementTimeoutQuery, tx.options.StatementTimeout); err != nil {
return errors.Wrap(err, "pig: set statement timeout")
}
}
return handler(&Ex{
ex: txx,
options: tx.options,
})
})
return errors.WithStack(err)
}