Remove AfterClose() and Conn() from Tx and Rows

batch-wip
Jack Christensen 2017-05-06 10:00:49 -05:00
parent 8322171bd8
commit 2a49569747
5 changed files with 26 additions and 99 deletions

View File

@ -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
}
}

View File

@ -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
View File

@ -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)
}
}
}

View File

@ -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()

8
v3.md
View File

@ -40,6 +40,14 @@ ConnPool.Close no longer waits for all acquired connections to be released. Inst
Removed Rows.Fatal(error)
Removed Rows.AfterClose()
Removed Rows.Conn()
Removed Tx.AfterClose()
Removed Tx.Conn()
## TODO / Possible / Investigate
Organize errors better