Why is it not creating a new connection, but using the old one?
I expected the Acquire function to return a free connection to me, but instead I get the same thing.
func (p *Pg) sendBatch(batch *pgx.Batch) error {
conn, err := p.pool.Acquire(context.Background())
log.Println(conn.Conn().PgConn().PID())
if err != nil {
return err
}
defer conn.Release()
br := conn.SendBatch(context.Background(), batch)
_, err = br.Exec()
log.Println(err)
return err
}
output:
db.go:77: 19320
db.go:88: <nil>
db.go:77: 19320
db.go:88: conn busy
db.go:77: 19320
db.go:88: conn busy
Batch operations must be closed before the connection can be used again. Unfortunately, that wasn't explicit in the docs. I've corrected that: e16bfa9af53e1ba5c007bdd7cce89cd5e1455518
The pool expects that the connection was returned in a idle state so it keeps returning the broken connection.
Call br.Close() and the problem will be resolved.
Also, I recommend calling SendBatch on the pool instead of acquiring and releasing a conn manually.
Most helpful comment
Batch operations must be closed before the connection can be used again. Unfortunately, that wasn't explicit in the docs. I've corrected that: e16bfa9af53e1ba5c007bdd7cce89cd5e1455518
The pool expects that the connection was returned in a idle state so it keeps returning the broken connection.
Call
br.Close()and the problem will be resolved.Also, I recommend calling SendBatch on the pool instead of acquiring and releasing a conn manually.