How to handle a postgres database connection reset with an open golang client connection

273 Views Asked by At

I have a bunch of golang microservices that connect to postgres using the https://github.com/jmoiron/sqlx/ package. When the service starts up, it connects to the database successfully and then gets a dedicated connection from the pool.

connPool, err := sqlx.Open("postgres", psqlInfo)
if err != nil {
    return nil, err
}
if err := connPool.Ping(); err != nil {
    return nil, err
}
conn, err := connPool.DB.Connx(context.Background())
if err != nil {
    return "", err
}

Then the code uses that connection pool in response to API calls. All works well.

The problem comes in if the postgres server is restarted while the golang service is still running.
On the first attempt to read from the database after it goes down and comes back up, we get a failure: Bad database connection detected. Attempting to recover: driver: bad connection On the second attempt, it works fine, sometimes. Other times I need to restart the service.

Why does it work find on the second attempt and not the first? Is there anyway to work around the restart? There are millions of db calls, I don't want to add a check before each one to make sure it is active.

Is there a setting or something I can change?

Thanks!

This seems to happen no matter if there is a dedicated connection from the pool or we open (and close) a connection from the pool for each database call.

0

There are 0 best solutions below