-
Notifications
You must be signed in to change notification settings - Fork 3
/
tx.go
34 lines (28 loc) · 902 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
package hermes
import (
"context"
"github.com/jackc/pgx/v4"
)
// Tx wraps the pgx.Tx interface and provides the missing hermes function wrappers.
type Tx struct {
pgx.Tx
}
// Begin starts a pseudo nested transaction.
func (tx *Tx) Begin(ctx context.Context) (Conn, error) {
newTx, err := tx.Tx.Begin(ctx)
if err != nil {
return nil, err
}
return &Tx{newTx}, nil
}
// Close rolls back the transaction if this is a real transaction or rolls back to the
// savepoint if this is a pseudo nested transaction.
//
// Returns ErrTxClosed if the Conn is already closed, but is otherwise safe to call multiple
// times. Hence, a defer conn.Close() is safe even if conn.Commit() will be called first in
// a non-error condition.
//
// Any other failure of a real transaction will result in the connection being closed.
func (tx *Tx) Close(ctx context.Context) error {
return tx.Tx.Rollback(ctx)
}