繁体   English   中英

阻止knex数据库连接池在空闲时关闭

[英]Prevent knex database connection pool from closing when idle

我正在使用Knex.js来处理我与数据库的连接。 我试图阻止连接池破坏空闲的连接。

我的配置看起来像这样

    {
      "client": "pg",
      "connection": {
        "host" : "localhost",
        "port" : "15432",
        "user" : "postgres",
        "password" : "",
        "database" : "postgres",
        "charset" : "utf8"
      },
      "pool": {
        "min" : 1,
        "max": 7,
        "idleTimeoutMillis": Number.MAX_SAFE_INTEGER
      },
      "migrations": {
        "directory": "app/database/migrations"
      }
    }

但是我仍然坚持下去

{"errno":"ETIMEDOUT","code":"ETIMEDOUT","syscall":"read"}

经过一段时间的不活动。

据我所知,当足够的时间过去时,连接应该从池中丢弃。 因此,如果连接暂时没有使用(这是我的情况),则池中将没有连接,并且我尝试的第一个调用应该在给定错误时失败。 后续调用顺利进行(直到新的超时)

我的问题是 - 如何防止这种情况?

编辑

在我的应用程序空闲一段时间后,必须转到数据库级别的第一个活动因给定错误而失败。 任何重复的通话都会成功。 这就是为什么我开始相信knex没有检测到连接被丢弃为空闲并且它没有及时重新连接以完成第一个查询。 我也相信问题出在knex方面而不是数据库方面。

所以我设法解决了

{"errno":"ETIMEDOUT","code":"ETIMEDOUT","syscall":"read"}

错误,通过这样的配置

{
  "client": "pg",
  "connection": {
    .
    .
    .      
  },
  "pool": { <- this is important
    "min" : 0 
  },
}

我在https://gist.github.com/acgourley/9a11ffedd44c414fb4b8上找到了建议的解决方案

事实上,我无法理解为什么这是一个解决方案以及为什么我之前的配置无效。

要注意重要的是,这些解决方案将无法正常工作

{
  "client": "pg",
  "connection": {
    .
    .
    .      
  },
  "pool": { 
    "min" : 0,
    "max" : 7 <- this fails in the same manner
  },
}

要么

{
  "client": "pg",
  "connection": {
    .
    .
    .      
  },
  "pool": { 
    "min" : 0,
    "max" : 7 <- this fails in the same manner
    "ping": () => {... ping function ...}
  },
}

所以对我来说这看起来像是在绕过一些现有的bug ......这个bug要么是knex,要么是tarn.js,要么是node-postgres。 或者,问题可能是我从根本上不了解JS数据库驱动程序如何工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM