dongyikong6207 2019-01-11 18:02
浏览 712

docker中的数据库连接超时

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 提问应符合社区要求 5月22日

    悬赏问题

    • ¥15 按键修改电子时钟,C51单片机
    • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
    • ¥20 5037端口被adb自己占了
    • ¥15 python:excel数据写入多个对应word文档
    • ¥60 全一数分解素因子和素数循环节位数
    • ¥15 ffmpeg如何安装到虚拟环境
    • ¥188 寻找能做王者评分提取的
    • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
    • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
    • ¥15 写论文,需要数据支撑