dqmg80654 2015-12-09 09:36
浏览 488
已采纳

在Go中关闭应用程序之前是否需要关闭数据库连接?

In Go, when using a SQL database, does one need to close the DB (db.Close) before closing an application? Will the DB automatically detect that the connection has died?

  • 写回答

4条回答 默认 最新

  • doushen9863 2015-12-09 10:24
    关注

    DB will do its best to detect but with no luck, it may not be able to detect. Better to release what is acquired as soon as possible.

    send() system call will wait for TCP connection to send data but client won't receive anything.

    1. Power failure, network issue or bare exit happened without properly releasing resources. TCP keepalive mechanism will kick in and try to detect that connection is dead.

    2. Client is paused and doesn't receive any data, in this case send() will block.

    As a result, it may prevent

    1. Graceful shutdown of cluster.
    2. Advancing event horizon if it was holding exclusive locks as a part of transaction such as auto vacuum in postgresql.

    Server keepalive config could be shortened to detect it earlier. (For example, ~2h 12m default in postgresql will be very long according to workload).

    There may be a hard limit on max open connections, until detection, some connections will be zombie (there, unusable but decreases limit).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题