-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.go
More file actions
78 lines (69 loc) · 1.73 KB
/
Copy pathtx.go
File metadata and controls
78 lines (69 loc) · 1.73 KB
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
package xpg
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
)
// InTx executes fn in a transaction configured by txOptions.
//
// If fn returns nil, the transaction is committed; otherwise it is rolled back.
// If fn panics, rollback is attempted before the panic is propagated. The
// callback must not call Commit or Rollback; InTx owns transaction
// finalization.
//
// The callback receives ctx unchanged. Context cancellation does not
// automatically finalize the transaction while fn is running; fn should
// observe ctx and return promptly.
func (p *Pool) InTx(
ctx context.Context,
txOptions pgx.TxOptions,
fn func(context.Context, pgx.Tx) error,
) error {
if fn == nil {
return errors.New("xpg: transaction function is nil")
}
err := pgx.BeginTxFunc(
ctx,
p.pool,
txOptions,
func(tx pgx.Tx) error {
return fn(ctx, tx)
},
)
if err != nil {
return fmt.Errorf("xpg: transaction: %w", err)
}
return nil
}
// InSavepoint executes fn within a PostgreSQL savepoint.
//
// If fn returns nil, the savepoint is released; otherwise it is rolled back.
// If fn panics, rollback is attempted before the panic is propagated. The
// callback must not call Commit or Rollback; InSavepoint owns savepoint
// finalization.
//
// The callback receives ctx unchanged and should observe its cancellation.
func InSavepoint(
ctx context.Context,
tx pgx.Tx,
fn func(context.Context, pgx.Tx) error,
) error {
if tx == nil {
return errors.New("xpg: transaction is nil")
}
if fn == nil {
return errors.New("xpg: savepoint function is nil")
}
err := pgx.BeginFunc(
ctx,
tx,
func(savepoint pgx.Tx) error {
return fn(ctx, savepoint)
},
)
if err != nil {
return fmt.Errorf("xpg: savepoint: %w", err)
}
return nil
}