mirror of
https://github.com/jackc/pgx.git
synced 2025-05-01 04:59:43 +00:00
It is impossible to guarantee that the a query executed with the simple protocol will behave the same as with the extended protocol. This is because the normal pgx path relies on knowing the OID of query parameters. Without this encoding a value can only be determined by the value instead of the combination of value and PostgreSQL type. For example, how should a []int32 be encoded? It might be encoded into a PostgreSQL int4[] or json. Removal also simplifies the core query path. The primary reason for the simple protocol is for servers like PgBouncer that may not be able to support normal prepared statements. After further research it appears that issuing a "flush" instead "sync" after preparing the unnamed statement would allow PgBouncer to work. The one round trip mode can be better handled with prepared statements. As a last resort, all original server functionality can still be accessed by dropping down to PgConn.
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package pool
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgconn"
|
|
"github.com/jackc/pgx"
|
|
"github.com/jackc/puddle"
|
|
)
|
|
|
|
// Conn is an acquired *pgx.Conn from a Pool.
|
|
type Conn struct {
|
|
res *puddle.Resource
|
|
}
|
|
|
|
// Release returns c to the pool it was acquired from. Once Release has been called, other methods must not be called.
|
|
// However, it is safe to call Release multiple times. Subsequent calls after the first will be ignored.
|
|
func (c *Conn) Release() {
|
|
if c.res == nil {
|
|
return
|
|
}
|
|
|
|
conn := c.Conn()
|
|
res := c.res
|
|
c.res = nil
|
|
|
|
go func() {
|
|
if !conn.IsAlive() {
|
|
res.Destroy()
|
|
return
|
|
}
|
|
|
|
if conn.TxStatus() != 'I' {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
_, err := conn.Exec(ctx, "rollback")
|
|
cancel()
|
|
if err != nil {
|
|
res.Destroy()
|
|
return
|
|
}
|
|
}
|
|
|
|
if conn.IsAlive() {
|
|
res.Release()
|
|
} else {
|
|
res.Destroy()
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
|
|
return c.Conn().Exec(ctx, sql, arguments...)
|
|
}
|
|
|
|
func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
|
|
return c.Conn().Query(ctx, sql, args...)
|
|
}
|
|
|
|
func (c *Conn) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
|
|
return c.Conn().QueryRow(ctx, sql, args...)
|
|
}
|
|
|
|
func (c *Conn) Begin() (*pgx.Tx, error) {
|
|
return c.Conn().Begin()
|
|
}
|
|
|
|
func (c *Conn) Conn() *pgx.Conn {
|
|
return c.res.Value().(*pgx.Conn)
|
|
}
|