Environment
- Environment: Mac and Docker
- Language: GoLang
- Database: Postgresql
Expected Behavior
We have a connection pool used to connect to database. The time of database connection may vary from 1 minute up to 20 minutes depending on the functions being called.
Actual Behavior
- Running in local: everything is fine.
-
Running in Docker: DB connection that is less than 5 minutes is fine. For DB connection that is longer than 5 minutes, every 5-6 minutes the connection is broken in docker and a new connection is initiated automatically. After 3 reconnections, following error is returned and process is terminated
driver: bad connection
Some Tries
- try to ping database during the function call to keep connection alive -- not works (working in some simple cases, but not in complicate case and not stable.
- try to ping database during function call with same connection using db.SetMaxOpenConns(1) and db.SetMaxIdleConns(1) -- not works.
- try to set connection life to longer with db.SetConnMaxLifetime() -- not works.
Some Info
After some search, the possible reason is that docker periodically checks connections and time them out if connection is alive for 300 seconds and no response returns. Not find any way to overwrite this configuration.
https://github.com/moby/moby/issues/36053
https://github.com/docker/for-mac/issues/2442
How can I get around this issue? Appreciate if any help.