brianc / node-pg-pool

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

Is a pool gracefully closing db connections when node.js exits? #102

Closed rmoestl closed 6 years ago

rmoestl commented 6 years ago

This is a question rather than a issue.

I wonder if pool is closing its connections when node.js is exiting for whatever reason. There is pool.end() but that's asynchronous which means that you can't call it in process.on('exit', callback) since node will terminate immediately afterwards without doing any further work in the event loop.

Calling pool.end() in the beforeExit event, that allows for asynchronous code, isn't a complete solution either, since beforeExit isn't called in some situations.

One could listen for the uncaughtException event, but in the respective callback you should

perform synchronous cleanup.

brianc commented 6 years ago

I wonder if pool is closing its connections when node.js is exiting for whatever reason.

The pool doesn't hook in to any node lifecycle events by default so it does nothing if node exits for whatever reason. Your connections to postgres are dropped as a by-product of the process exiting.

If you want to end the pool calling pool.end yourself is the way to go; however, it's not a big deal if your process exits and the connections drop - postgres is fine with this. Using pool.end is more for when you need a disconnect in your app - either for tests to end properly or to make sure your pool is closed before opening a new one or something similar.

rmoestl commented 6 years ago

Thanks for clarifying!