【问题标题】:Closing idle connections: Does Sequelize allow me to view how long a DB connection has been idle?关闭空闲连接:Sequelize 是否允许我查看数据库连接空闲多长时间?
【发布时间】:2021-07-10 00:04:44
【问题描述】:

我的应用服务器为每个租户使用一个数据库,我需要一次连接到多个数据库。为了使用 Sequelize 做到这一点,我维护了一个数据库池,我在其中添加了到 Map 的连接:

addConnection(dbKey) {
  const sequelize = new Sequelize(
    dbKey,
    "username",
    "password",
    {
      host: localhost,
      port: 5000,
      schema: 'main',
      dialect: 'postgres'
    }
  );

  const models = {
    sequelize: sequelize,
    customerData: CustomerData(sequelize, DataTypes)
  }
  
  this.dbPool.set(dbKey, models);
}

这使我可以根据需要检索续集实例。目前,我没有看到对管理与多个数据库的连接的单个 sequelize 实例的任何支持。而且因为我正在维护自己的连接图,所以我似乎需要执行释放空闲连接的任务。

如果在一定时间间隔内未使用空闲连接,我想关闭它们并将它们从 Map 中删除。我考虑了几种方法,但不知道哪种方法可行。它真的归结为 2..

1 - 为 Sequelize 实例设置 options.pool.idle。

> The maximum time, in milliseconds, that a connection can be idle before being released.

AFAIK 这不符合我的要求,它只会在同一数据库存在连接池时设置超时,所以这显然行不通。

2 - 使用 SetInterval 关闭连接并在空闲时从 Map 对象中删除它们的引用。

a) When opening connection, set a timestamp along with connection.

b) Whenever the sequelize instance is used to make a query, update the timestamp.

c) Every minute or so, close and free any connections that have been idle for longer than some time period.

如果 Sequelize 或 getter 中有一些内部字段可以让我看到上次使用连接的时间,那就太好了。查看 Sequelize 和模型实例的 API,我没有看到这样的字段或方法。有没有办法确定 Sequelize 库中上次使用连接的时间?

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    当您创建一个新的 Sequelize 实例时,它会默认在后台创建一个池。
    您可以查看可用于实例初始化的options.pool 对象。
    https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor

    const sequelize = new Sequelize(
        dbKey,
        "username",
        "password",
        {
            host: localhost,
            port: 5000,
            schema: 'main',
            dialect: 'postgres',
            pool: {
                idle: 10000, // milliseconds
                evict: 20000, // milliseconds
            }
        }
    );
    

    默认值如下:

    公共构造函数(数据库:字符串,用户名:字符串,密码:字符串,选项:对象)

    Name Type Attribute Description
    options.pool object optional sequelize connection pool configuration
    options.pool.max number optional, default: 5 Maximum number of connection in pool
    options.pool.min number optional, default: 0 Minimum number of connection in pool
    options.pool.idle number optional, default: 10000 The maximum time, in milliseconds, that a connection can be idle before being released.
    options.pool.acquire number optional, default: 60000 The maximum time, in milliseconds, that pool will try to get connection before throwing error
    options.pool.evict number optional, default: 1000 The time interval, in milliseconds, after which sequelize-pool will remove idle connections.
    options.pool.validate Function optional A function that validates a connection. Called with client. The default function checks that client is an object, and that its state is not disconnected
    options.pool.maxUses number optional, default: Infinity The number of times a connection can be used before discarding it for a replacement, used for eventual cluster rebalancing.

    因此它会根据上面的默认值或您提供的值自动释放和获取连接。

    因此,假设在 10000 毫秒内没有执行任何查询,然后按照默认值 options.pool.idle 释放连接。

    因此,当一段时间内没有触发查询时,sequelize 实例就在那里,没有任何实时数据库连接。因此,如果没问题,您可以保持原样。或者可以使用下面的连接挂钩来识别连接并维护一个计数器以从您的地图中初始化/删除 Sequelize 实例。

    您可以在此处查看连接挂钩文档: https://sequelize.org/master/manual/hooks.html#connection-hooks

    【讨论】:

    • 谢谢。我太挑剔了。我将使用空闲和驱逐选项,除非地图在膨胀方面成为问题。我担心 Sequelize 实例太多,我不确定说 100 或 1000 会如何影响性能。
    猜你喜欢
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2012-02-20
    • 1970-01-01
    • 2012-10-25
    • 2020-04-29
    • 2020-02-09
    相关资源
    最近更新 更多