Skip to content

Commit 837704d

Browse files
committed
Update stdlib docs to point to Conn.Raw instead of AcquireConn
1 parent 2bd26ec commit 837704d

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

stdlib/sql.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,23 @@
2828
//
2929
// db.QueryRow("select * from users where id=$1", userID)
3030
//
31-
// AcquireConn and ReleaseConn acquire and release a *pgx.Conn from the standard
32-
// database/sql.DB connection pool. This allows operations that must be
33-
// performed on a single connection without running in a transaction, and it
34-
// supports operations that use pgx specific functionality.
31+
// In Go 1.13 and above (*sql.Conn) Raw() can be used to get a *pgx.Conn from the standard
32+
// database/sql.DB connection pool. This allows operations that use pgx specific functionality.
3533
//
36-
// conn, err := stdlib.AcquireConn(db)
34+
// // Given db is a *sql.DB
35+
// conn, err := db.Conn(context.Background())
3736
// if err != nil {
38-
// return err
37+
// // handle error from acquiring connection from DB pool
3938
// }
40-
// defer stdlib.ReleaseConn(db, conn)
41-
//
42-
// // do stuff with pgx.Conn
43-
//
44-
// It also can be used to enable a fast path for pgx while preserving
45-
// compatibility with other drivers and database.
4639
//
47-
// conn, err := stdlib.AcquireConn(db)
48-
// if err == nil {
49-
// // fast path with pgx
50-
// // ...
51-
// // release conn when done
52-
// stdlib.ReleaseConn(db, conn)
53-
// } else {
54-
// // normal path for other drivers and databases
40+
// err = conn.Raw(func(driverConn interface{}) error {
41+
// conn := driverConn.(*stdlib.Conn).Conn() // conn is a *pgx.Conn
42+
// // Do pgx specific stuff with conn
43+
// conn.CopyFrom(...)
44+
// return nil
45+
// })
46+
// if err != nil {
47+
// // handle error that occurred while using *pgx.Conn
5548
// }
5649
package stdlib
5750

@@ -698,6 +691,9 @@ func (fakeTx) Commit() error { return nil }
698691

699692
func (fakeTx) Rollback() error { return nil }
700693

694+
// AcquireConn acquires a *pgx.Conn from database/sql connection pool. It must be released with ReleaseConn.
695+
//
696+
// In Go 1.13 this functionality has been incorporated into the standard library in the db.Conn.Raw() method.
701697
func AcquireConn(db *sql.DB) (*pgx.Conn, error) {
702698
var conn *pgx.Conn
703699
ctx := context.WithValue(context.Background(), ctxKeyFakeTx, &conn)
@@ -717,6 +713,7 @@ func AcquireConn(db *sql.DB) (*pgx.Conn, error) {
717713
return conn, nil
718714
}
719715

716+
// ReleaseConn releases a *pgx.Conn acquired with AcquireConn.
720717
func ReleaseConn(db *sql.DB, conn *pgx.Conn) error {
721718
var tx *sql.Tx
722719
var ok bool

0 commit comments

Comments
 (0)