Hey I'm getting an error message : conn busy
from pgx
I don't know how to solve this. Here is my function :
func (r *proverbRepo) SelectPendingProverbs(table string) (proverbs []domain.Proverb, err error) {
query := fmt.Sprintf("SELECT id, proverb literal FROM %s", table)
rows, err := r.Db.Query(context.Background(), query)
defer rows.Close()
if err != nil {
return
}
for rows.Next() {
var prov domain.Proverb
if err = rows.Scan(&prov.ID, &prov.Literal); err != nil {
return
}
proverbs = append(proverbs, prov)
}
return
}
r.Db
is pgx.Connect(context.Background(), os.Getenv("PSQL_URL"))
I'm fetching two different table in a very short interval from two separate front end requests.
The first request goes through, the other one returns the conn busy
error message.
I really don't know what to look for, would somebody help me ?
pgx.Connect()
returns a pgx.Conn
which cannot be used concurrently. This is what the godocs of this type state:
Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. Use a connection pool to manage access to multiple database connections from multiple goroutines.
So if you replace pgx.Connect()
with pgxpool.Connect()
from github.com/jackc/pgx/pgxpool
you should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With