Closed Darker closed 2 weeks ago
There are kind of an annoying amount of places you need to add event listeners to prevent this. I'm going to adjust error handling to be easier in the future...for now you'll need to make sure you have an error handler added to the pool instance (if you're using a connection pool). Furthermore any manually created & connected clients which are idling must also have an error listener attached. Something like this should work:
const pool = new pg.Pool()
pool.on('error', () => {
// error in background w/ idle clients in pool
})
// or if you have manual clients:
const client = new pg.Client()
await client.connect()
client.on('error', () => {
// error on client while it was idle but connected to database
})
Those are the two places I believe you'll need an error event listener. Pls lmk if that doesn't work or you run into issuese with that. Sorry for the headache. I'm adjusting things in the future to where you wont need to have error listeners but I'm still working out the details (and it will be in a new semver major or opt-in only behavior since its a breaking, though good, change)
I have a simple express web app that runs an utility server. The whole app is basically just
pg
connection and a few Express json endpoints.I used to have an issue where the database side would auto disconnect and the whole server would crash because of an error in pg. This is now occuring again. I think on the database server, some timeout was added which is kicking my app out.
This crash does not occur during a request but when the server is idle. How can I handle the forced disconnect so that nodejs keeps running?
Here's what it looks like:
I want to handle any errors myself, probably by reconnecting. I am currently only using the
pg
package: