简体   繁体   English

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

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

I am using Knex.js to handle my connections to database. 我正在使用Knex.js来处理我与数据库的连接。 I am trying to prevent connection pool from destroying the connections that were idle. 我试图阻止连接池破坏空闲的连接。

My config looks like this 我的配置看起来像这样

    {
      "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"
      }
    }

However I still keep getting 但是我仍然坚持下去

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

After a period of inactivity. 经过一段时间的不活动。

To my knowledge, when enough time passes a connection should be thrown away from the pool. 据我所知,当足够的时间过去时,连接应该从池中丢弃。 So if the connections are not used for a while (that is my case) there will be no connections in the pool and the first call I try should fail with given error. 因此,如果连接暂时没有使用(这是我的情况),则池中将没有连接,并且我尝试的第一个调用应该在给定错误时失败。 Subsequent calls go trough smoothly (until new timeout) 后续调用顺利进行(直到新的超时)

My question is - how to prevent this? 我的问题是 - 如何防止这种情况?

EDIT 编辑

after my app is idle for some time, first activity that has to go to the database level fails with given error. 在我的应用程序空闲一段时间后,必须转到数据库级别的第一个活动因给定错误而失败。 Any repeated call will be successful. 任何重复的通话都会成功。 That is why I came to believe that knex does not detect that a connection is being discarded as idle and it does not reconnect in time for the first query to be finished. 这就是为什么我开始相信knex没有检测到连接被丢弃为空闲并且它没有及时重新连接以完成第一个查询。 I also believe that the problem is on the knex side and not on the database side. 我也相信问题出在knex方面而不是数据库方面。

So I managed to fix 所以我设法解决了

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

errors, by making the configuration like this 错误,通过这样的配置

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

I found the suggested solution on https://gist.github.com/acgourley/9a11ffedd44c414fb4b8 我在https://gist.github.com/acgourley/9a11ffedd44c414fb4b8上找到了建议的解决方案

Thing is, I did not manage to understand why is this a solution and why my previous configuration was not working. 事实上,我无法理解为什么这是一个解决方案以及为什么我之前的配置无效。

Important thing to notice is that these solutions will not work 要注意重要的是,这些解决方案将无法正常工作

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

or 要么

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

So to me this looks like circumventing some existing bug... The bug is either in knex or tarn.js or in node-postgres. 所以对我来说这看起来像是在绕过一些现有的bug ......这个bug要么是knex,要么是tarn.js,要么是node-postgres。 Or, the issue might be that I fundamentally do not understand how JS database drivers work. 或者,问题可能是我从根本上不了解JS数据库驱动程序如何工作。

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

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