brianc / node-pg-pool

A connection pool for node-postgres
MIT License
180 stars 64 forks source link

Returning client to pool after error #48

Closed maxpain closed 7 years ago

maxpain commented 7 years ago

If query throws error, then client not returning to connection pool. Example:

const pool = new pg.Pool({ max: 1 })

try {
    await pool.query(`invalid sql`)
} catch (e) {
    console.log(e)
}

await pool.query('Another query') // Never ends
bobbysmith007 commented 7 years ago

This is a problem in generic pool 2.4.2 which is used as a requirement. There is already a ticket to move to generic-pool 3: https://github.com/brianc/node-pg-pool/issues/29

I added a ticket to fix this bug in https://github.com/coopernurse/node-pool/pull/186 You can also route around it yourself, by specifying a ("min": 3) in your pool config object.

bobbysmith007 commented 7 years ago

I also monkey patched it, if that is an approach that helps folks get through https://github.com/strongloop/loopback-connector-postgresql/pull/234

bobbysmith007 commented 7 years ago

Its worth noting that some errors will require the pooled object to be recreated and some we could return the object to the pool. Not sure if it is worth it to try and distinguish between the two

brianc commented 7 years ago

Hey @bobbysmith007 that's a gross bug - I'm sorry about that! Would you be willing to put together a pull request w/ a failing test for this repo? Until pg@7.x and pg-pool@2.x I don't want to break backwards compat w/ node >= 0.10.x so I'd be nice to have that work around here before the upgrade. If there's a PR with a failing test I'd likely be able to write a workaround here based on your work in the loopback connector.

bobbysmith007 commented 7 years ago

Yes I can! Out till Monday but I will get it up soon.