mirror of https://github.com/jackc/pgx.git
Remove AfterClose() and Conn() from Tx and Rows
parent
8322171bd8
commit
2a49569747
16
conn_pool.go
16
conn_pool.go
|
@ -31,8 +31,6 @@ type ConnPool struct {
|
|||
preparedStatements map[string]*PreparedStatement
|
||||
acquireTimeout time.Duration
|
||||
connInfo *pgtype.ConnInfo
|
||||
txAfterClose func(tx *Tx)
|
||||
rowsAfterClose func(rows *Rows)
|
||||
}
|
||||
|
||||
type ConnPoolStat struct {
|
||||
|
@ -75,14 +73,6 @@ func NewConnPool(config ConnPoolConfig) (p *ConnPool, err error) {
|
|||
p.logLevel = LogLevelNone
|
||||
}
|
||||
|
||||
p.txAfterClose = func(tx *Tx) {
|
||||
p.Release(tx.Conn())
|
||||
}
|
||||
|
||||
p.rowsAfterClose = func(rows *Rows) {
|
||||
p.Release(rows.Conn())
|
||||
}
|
||||
|
||||
p.allConnections = make([]*Conn, 0, p.maxConnections)
|
||||
p.availableConnections = make([]*Conn, 0, p.maxConnections)
|
||||
p.preparedStatements = make(map[string]*PreparedStatement)
|
||||
|
@ -381,7 +371,7 @@ func (p *ConnPool) Query(sql string, args ...interface{}) (*Rows, error) {
|
|||
return rows, err
|
||||
}
|
||||
|
||||
rows.AfterClose(p.rowsAfterClose)
|
||||
rows.connPool = p
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
@ -399,7 +389,7 @@ func (p *ConnPool) QueryEx(ctx context.Context, sql string, options *QueryExOpti
|
|||
return rows, err
|
||||
}
|
||||
|
||||
rows.AfterClose(p.rowsAfterClose)
|
||||
rows.connPool = p
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
@ -531,7 +521,7 @@ func (p *ConnPool) BeginEx(txOptions *TxOptions) (*Tx, error) {
|
|||
continue
|
||||
}
|
||||
|
||||
tx.AfterClose(p.txAfterClose)
|
||||
tx.connPool = p
|
||||
return tx, nil
|
||||
}
|
||||
}
|
||||
|
|
25
query.go
25
query.go
|
@ -42,6 +42,7 @@ func (r *Row) Scan(dest ...interface{}) (err error) {
|
|||
// calling Next() until it returns false, or when a fatal error occurs.
|
||||
type Rows struct {
|
||||
conn *Conn
|
||||
connPool *ConnPool
|
||||
values [][]byte
|
||||
fields []FieldDescription
|
||||
rowCount int
|
||||
|
@ -50,7 +51,6 @@ type Rows struct {
|
|||
startTime time.Time
|
||||
sql string
|
||||
args []interface{}
|
||||
afterClose func(*Rows)
|
||||
unlockConn bool
|
||||
closed bool
|
||||
}
|
||||
|
@ -84,8 +84,8 @@ func (rows *Rows) Close() {
|
|||
rows.conn.log(LogLevelError, "Query", map[string]interface{}{"sql": rows.sql, "args": logQueryArgs(rows.args)})
|
||||
}
|
||||
|
||||
if rows.afterClose != nil {
|
||||
rows.afterClose(rows)
|
||||
if rows.connPool != nil {
|
||||
rows.connPool.Release(rows.conn)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -156,11 +156,6 @@ func (rows *Rows) Next() bool {
|
|||
}
|
||||
}
|
||||
|
||||
// Conn returns the *Conn this *Rows is using.
|
||||
func (rows *Rows) Conn() *Conn {
|
||||
return rows.conn
|
||||
}
|
||||
|
||||
func (rows *Rows) nextColumn() ([]byte, *FieldDescription, bool) {
|
||||
if rows.closed {
|
||||
return nil, nil, false
|
||||
|
@ -321,20 +316,6 @@ func (rows *Rows) Values() ([]interface{}, error) {
|
|||
return values, rows.Err()
|
||||
}
|
||||
|
||||
// AfterClose adds f to a LILO queue of functions that will be called when
|
||||
// rows is closed.
|
||||
func (rows *Rows) AfterClose(f func(*Rows)) {
|
||||
if rows.afterClose == nil {
|
||||
rows.afterClose = f
|
||||
} else {
|
||||
prevFn := rows.afterClose
|
||||
rows.afterClose = func(rows *Rows) {
|
||||
f(rows)
|
||||
prevFn(rows)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Query executes sql with args. If there is an error the returned *Rows will
|
||||
// be returned in an error state. So it is allowed to ignore the error returned
|
||||
// from Query and handle it in *Rows.
|
||||
|
|
37
tx.go
37
tx.go
|
@ -94,10 +94,10 @@ func (c *Conn) BeginEx(txOptions *TxOptions) (*Tx, error) {
|
|||
// All Tx methods return ErrTxClosed if Commit or Rollback has already been
|
||||
// called on the Tx.
|
||||
type Tx struct {
|
||||
conn *Conn
|
||||
afterClose func(*Tx)
|
||||
err error
|
||||
status int8
|
||||
conn *Conn
|
||||
connPool *ConnPool
|
||||
err error
|
||||
status int8
|
||||
}
|
||||
|
||||
// Commit commits the transaction
|
||||
|
@ -117,9 +117,10 @@ func (tx *Tx) Commit() error {
|
|||
tx.err = err
|
||||
}
|
||||
|
||||
if tx.afterClose != nil {
|
||||
tx.afterClose(tx)
|
||||
if tx.connPool != nil {
|
||||
tx.connPool.Release(tx.conn)
|
||||
}
|
||||
|
||||
return tx.err
|
||||
}
|
||||
|
||||
|
@ -139,9 +140,10 @@ func (tx *Tx) Rollback() error {
|
|||
tx.status = TxStatusRollbackFailure
|
||||
}
|
||||
|
||||
if tx.afterClose != nil {
|
||||
tx.afterClose(tx)
|
||||
if tx.connPool != nil {
|
||||
tx.connPool.Release(tx.conn)
|
||||
}
|
||||
|
||||
return tx.err
|
||||
}
|
||||
|
||||
|
@ -194,11 +196,6 @@ func (tx *Tx) CopyFrom(tableName Identifier, columnNames []string, rowSrc CopyFr
|
|||
return tx.conn.CopyFrom(tableName, columnNames, rowSrc)
|
||||
}
|
||||
|
||||
// Conn returns the *Conn this transaction is using.
|
||||
func (tx *Tx) Conn() *Conn {
|
||||
return tx.conn
|
||||
}
|
||||
|
||||
// Status returns the status of the transaction from the set of
|
||||
// pgx.TxStatus* constants.
|
||||
func (tx *Tx) Status() int8 {
|
||||
|
@ -209,17 +206,3 @@ func (tx *Tx) Status() int8 {
|
|||
func (tx *Tx) Err() error {
|
||||
return tx.err
|
||||
}
|
||||
|
||||
// AfterClose adds f to a LILO queue of functions that will be called when
|
||||
// the transaction is closed (either Commit or Rollback).
|
||||
func (tx *Tx) AfterClose(f func(*Tx)) {
|
||||
if tx.afterClose == nil {
|
||||
tx.afterClose = f
|
||||
} else {
|
||||
prevFn := tx.afterClose
|
||||
tx.afterClose = func(tx *Tx) {
|
||||
f(tx)
|
||||
prevFn(tx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
39
tx_test.go
39
tx_test.go
|
@ -1,9 +1,9 @@
|
|||
package pgx_test
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx"
|
||||
)
|
||||
|
||||
func TestTransactionSuccessfulCommit(t *testing.T) {
|
||||
|
@ -226,41 +226,6 @@ func TestBeginExReadOnly(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTxAfterClose(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := mustConnect(t, *defaultConnConfig)
|
||||
defer closeConn(t, conn)
|
||||
|
||||
tx, err := conn.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var zeroTime, t1, t2 time.Time
|
||||
tx.AfterClose(func(tx *pgx.Tx) {
|
||||
t1 = time.Now()
|
||||
})
|
||||
|
||||
tx.AfterClose(func(tx *pgx.Tx) {
|
||||
t2 = time.Now()
|
||||
})
|
||||
|
||||
tx.Rollback()
|
||||
|
||||
if t1 == zeroTime {
|
||||
t.Error("First Tx.AfterClose callback not called")
|
||||
}
|
||||
|
||||
if t2 == zeroTime {
|
||||
t.Error("Second Tx.AfterClose callback not called")
|
||||
}
|
||||
|
||||
if t1.Before(t2) {
|
||||
t.Errorf("AfterClose callbacks called out of order: %v, %v", t1, t2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTxStatus(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
Loading…
Reference in New Issue