-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.go
51 lines (41 loc) · 1.15 KB
/
db.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
package hermes
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
// DB wraps the *pgxpool.Pool and provides the missing hermes function wrappers.
type DB struct {
*pgxpool.Pool
defaultTimeout time.Duration
}
// Begin a new transaction.
func (db *DB) Begin(ctx context.Context) (Conn, error) {
if ctx == nil {
ctx = context.Background()
}
tx, err := db.Pool.Begin(ctx)
if err != nil {
return nil, err
}
return &Tx{tx, db.defaultTimeout}, nil
}
// Commit does nothing.
func (db *DB) Commit(context.Context) error {
return nil
}
// Rollback does nothing
func (db *DB) Rollback(context.Context) error {
return nil
}
// Close does nothing. Since this Close method is meant to be used interchangably with
// transactions, it doesn't actually close anything, because we don't want to close the underlying
// database pool at the end of every non-transactional request. Instead, see DB.Shutdown.
func (db *DB) Close(context.Context) error {
return nil
}
// Shutdown the underlying pgx Pool. You should call this when your application is closing to
// release all the database pool connections.
func (db *DB) Shutdown() {
db.Pool.Close()
}