mirror of https://github.com/jackc/pgx.git
Add error to *ConnectionPool.Acquire return
parent
209a9dfaf5
commit
622ff142ca
|
@ -610,7 +610,10 @@ func BenchmarkConnectionPool(b *testing.B) {
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
conn := pool.Acquire()
|
var conn *pgx.Connection
|
||||||
|
if conn, err = pool.Acquire(); err != nil {
|
||||||
|
b.Fatalf("Unable to acquire connection: %v", err)
|
||||||
|
}
|
||||||
pool.Release(conn)
|
pool.Release(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,8 @@ type ConnectionPoolOptions struct {
|
||||||
type ConnectionPool struct {
|
type ConnectionPool struct {
|
||||||
connectionChannel chan *Connection
|
connectionChannel chan *Connection
|
||||||
parameters ConnectionParameters // parameters used when establishing connection
|
parameters ConnectionParameters // parameters used when establishing connection
|
||||||
options ConnectionPoolOptions
|
maxConnections int
|
||||||
|
afterConnect func(*Connection) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConnectionPool creates a new ConnectionPool. parameters are passed through to
|
// NewConnectionPool creates a new ConnectionPool. parameters are passed through to
|
||||||
|
@ -18,9 +19,10 @@ func NewConnectionPool(parameters ConnectionParameters, options ConnectionPoolOp
|
||||||
p.connectionChannel = make(chan *Connection, options.MaxConnections)
|
p.connectionChannel = make(chan *Connection, options.MaxConnections)
|
||||||
|
|
||||||
p.parameters = parameters
|
p.parameters = parameters
|
||||||
p.options = options
|
p.maxConnections = options.MaxConnections
|
||||||
|
p.afterConnect = options.AfterConnect
|
||||||
|
|
||||||
for i := 0; i < p.options.MaxConnections; i++ {
|
for i := 0; i < p.maxConnections; i++ {
|
||||||
var c *Connection
|
var c *Connection
|
||||||
c, err = p.createConnection()
|
c, err = p.createConnection()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -33,7 +35,7 @@ func NewConnectionPool(parameters ConnectionParameters, options ConnectionPoolOp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Acquire takes exclusive use of a connection until it is released.
|
// Acquire takes exclusive use of a connection until it is released.
|
||||||
func (p *ConnectionPool) Acquire() (c *Connection) {
|
func (p *ConnectionPool) Acquire() (c *Connection, err error) {
|
||||||
c = <-p.connectionChannel
|
c = <-p.connectionChannel
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -48,7 +50,7 @@ func (p *ConnectionPool) Release(c *Connection) {
|
||||||
|
|
||||||
// Close ends the use of a connection by closing all underlying connections.
|
// Close ends the use of a connection by closing all underlying connections.
|
||||||
func (p *ConnectionPool) Close() {
|
func (p *ConnectionPool) Close() {
|
||||||
for i := 0; i < p.options.MaxConnections; i++ {
|
for i := 0; i < p.maxConnections; i++ {
|
||||||
c := <-p.connectionChannel
|
c := <-p.connectionChannel
|
||||||
_ = c.Close()
|
_ = c.Close()
|
||||||
}
|
}
|
||||||
|
@ -59,8 +61,8 @@ func (p *ConnectionPool) createConnection() (c *Connection, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if p.options.AfterConnect != nil {
|
if p.afterConnect != nil {
|
||||||
err = p.options.AfterConnect(c)
|
err = p.afterConnect(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -70,7 +72,10 @@ func (p *ConnectionPool) createConnection() (c *Connection, err error) {
|
||||||
|
|
||||||
// SelectFunc acquires a connection, delegates the call to that connection, and releases the connection
|
// SelectFunc acquires a connection, delegates the call to that connection, and releases the connection
|
||||||
func (p *ConnectionPool) SelectFunc(sql string, onDataRow func(*DataRowReader) error, arguments ...interface{}) (err error) {
|
func (p *ConnectionPool) SelectFunc(sql string, onDataRow func(*DataRowReader) error, arguments ...interface{}) (err error) {
|
||||||
c := p.Acquire()
|
var c *Connection
|
||||||
|
if c, err = p.Acquire(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
defer p.Release(c)
|
defer p.Release(c)
|
||||||
|
|
||||||
return c.SelectFunc(sql, onDataRow, arguments...)
|
return c.SelectFunc(sql, onDataRow, arguments...)
|
||||||
|
@ -78,7 +83,10 @@ func (p *ConnectionPool) SelectFunc(sql string, onDataRow func(*DataRowReader) e
|
||||||
|
|
||||||
// SelectRows acquires a connection, delegates the call to that connection, and releases the connection
|
// SelectRows acquires a connection, delegates the call to that connection, and releases the connection
|
||||||
func (p *ConnectionPool) SelectRows(sql string, arguments ...interface{}) (rows []map[string]interface{}, err error) {
|
func (p *ConnectionPool) SelectRows(sql string, arguments ...interface{}) (rows []map[string]interface{}, err error) {
|
||||||
c := p.Acquire()
|
var c *Connection
|
||||||
|
if c, err = p.Acquire(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
defer p.Release(c)
|
defer p.Release(c)
|
||||||
|
|
||||||
return c.SelectRows(sql, arguments...)
|
return c.SelectRows(sql, arguments...)
|
||||||
|
@ -86,7 +94,10 @@ func (p *ConnectionPool) SelectRows(sql string, arguments ...interface{}) (rows
|
||||||
|
|
||||||
// SelectRow acquires a connection, delegates the call to that connection, and releases the connection
|
// SelectRow acquires a connection, delegates the call to that connection, and releases the connection
|
||||||
func (p *ConnectionPool) SelectRow(sql string, arguments ...interface{}) (row map[string]interface{}, err error) {
|
func (p *ConnectionPool) SelectRow(sql string, arguments ...interface{}) (row map[string]interface{}, err error) {
|
||||||
c := p.Acquire()
|
var c *Connection
|
||||||
|
if c, err = p.Acquire(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
defer p.Release(c)
|
defer p.Release(c)
|
||||||
|
|
||||||
return c.SelectRow(sql, arguments...)
|
return c.SelectRow(sql, arguments...)
|
||||||
|
@ -94,7 +105,10 @@ func (p *ConnectionPool) SelectRow(sql string, arguments ...interface{}) (row ma
|
||||||
|
|
||||||
// SelectValue acquires a connection, delegates the call to that connection, and releases the connection
|
// SelectValue acquires a connection, delegates the call to that connection, and releases the connection
|
||||||
func (p *ConnectionPool) SelectValue(sql string, arguments ...interface{}) (v interface{}, err error) {
|
func (p *ConnectionPool) SelectValue(sql string, arguments ...interface{}) (v interface{}, err error) {
|
||||||
c := p.Acquire()
|
var c *Connection
|
||||||
|
if c, err = p.Acquire(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
defer p.Release(c)
|
defer p.Release(c)
|
||||||
|
|
||||||
return c.SelectValue(sql, arguments...)
|
return c.SelectValue(sql, arguments...)
|
||||||
|
@ -102,7 +116,10 @@ func (p *ConnectionPool) SelectValue(sql string, arguments ...interface{}) (v in
|
||||||
|
|
||||||
// SelectValues acquires a connection, delegates the call to that connection, and releases the connection
|
// SelectValues acquires a connection, delegates the call to that connection, and releases the connection
|
||||||
func (p *ConnectionPool) SelectValues(sql string, arguments ...interface{}) (values []interface{}, err error) {
|
func (p *ConnectionPool) SelectValues(sql string, arguments ...interface{}) (values []interface{}, err error) {
|
||||||
c := p.Acquire()
|
var c *Connection
|
||||||
|
if c, err = p.Acquire(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
defer p.Release(c)
|
defer p.Release(c)
|
||||||
|
|
||||||
return c.SelectValues(sql, arguments...)
|
return c.SelectValues(sql, arguments...)
|
||||||
|
@ -110,7 +127,10 @@ func (p *ConnectionPool) SelectValues(sql string, arguments ...interface{}) (val
|
||||||
|
|
||||||
// Execute acquires a connection, delegates the call to that connection, and releases the connection
|
// Execute acquires a connection, delegates the call to that connection, and releases the connection
|
||||||
func (p *ConnectionPool) Execute(sql string, arguments ...interface{}) (commandTag string, err error) {
|
func (p *ConnectionPool) Execute(sql string, arguments ...interface{}) (commandTag string, err error) {
|
||||||
c := p.Acquire()
|
var c *Connection
|
||||||
|
if c, err = p.Acquire(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
defer p.Release(c)
|
defer p.Release(c)
|
||||||
|
|
||||||
return c.Execute(sql, arguments...)
|
return c.Execute(sql, arguments...)
|
||||||
|
|
|
@ -57,7 +57,10 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) {
|
||||||
acquireAll := func() (connections []*pgx.Connection) {
|
acquireAll := func() (connections []*pgx.Connection) {
|
||||||
connections = make([]*pgx.Connection, maxConnections)
|
connections = make([]*pgx.Connection, maxConnections)
|
||||||
for i := 0; i < maxConnections; i++ {
|
for i := 0; i < maxConnections; i++ {
|
||||||
connections[i] = pool.Acquire()
|
var err error
|
||||||
|
if connections[i], err = pool.Acquire(); err != nil {
|
||||||
|
t.Fatalf("Unable to acquire connection: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -74,8 +77,7 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
f := func() {
|
f := func() {
|
||||||
var err error
|
conn, err := pool.Acquire()
|
||||||
conn := pool.Acquire()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to acquire connection")
|
t.Fatal("Unable to acquire connection")
|
||||||
}
|
}
|
||||||
|
@ -123,8 +125,10 @@ func TestPoolReleaseWithTransactions(t *testing.T) {
|
||||||
pool := createConnectionPool(t, 1)
|
pool := createConnectionPool(t, 1)
|
||||||
defer pool.Close()
|
defer pool.Close()
|
||||||
|
|
||||||
var err error
|
conn, err := pool.Acquire()
|
||||||
conn := pool.Acquire()
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to acquire connection: %v", err)
|
||||||
|
}
|
||||||
mustExecute(t, conn, "begin")
|
mustExecute(t, conn, "begin")
|
||||||
if _, err = conn.Execute("select"); err == nil {
|
if _, err = conn.Execute("select"); err == nil {
|
||||||
t.Fatal("Did not receive expected error")
|
t.Fatal("Did not receive expected error")
|
||||||
|
@ -139,7 +143,10 @@ func TestPoolReleaseWithTransactions(t *testing.T) {
|
||||||
t.Fatalf("Expected release to rollback errored transaction, but it did not: '%c'", conn.TxStatus)
|
t.Fatalf("Expected release to rollback errored transaction, but it did not: '%c'", conn.TxStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn = pool.Acquire()
|
conn, err = pool.Acquire()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to acquire connection: %v", err)
|
||||||
|
}
|
||||||
mustExecute(t, conn, "begin")
|
mustExecute(t, conn, "begin")
|
||||||
if conn.TxStatus != 'T' {
|
if conn.TxStatus != 'T' {
|
||||||
t.Fatalf("Expected txStatus to be 'T', instead it was '%c'", conn.TxStatus)
|
t.Fatalf("Expected txStatus to be 'T', instead it was '%c'", conn.TxStatus)
|
||||||
|
|
Loading…
Reference in New Issue