【问题标题】:@mysql/xdevapi ECONNREFUSED doesn't release connection@mysql/xdevapi ECONNREFUSED 不释放连接
【发布时间】:2020-12-07 19:18:30
【问题描述】:

我正在使用带有本地安装 mysql-8.0.15-winx64 的 @mysql/xdevapi npm 包(版本 8.0.22)。

我已将池设置为启用并尝试从客户端检索会话。如果我在 mysql 准备好之前执行此操作,那么我会收到一个 ECONNREFUSED 异常,这是预期的,但连接似乎永远不会被释放。如果池大小为 1,则所有后续尝试获取会话

异常是从 getSession 方法中引发的,因此不会返回会话让我手动调用 .end()

const mysqlx = require('@mysql/xdevapi');
const client = mysqlx.getClient(config, { pooling: { enabled: true, maxSize: 1, queueTimeout: 2000 } });
const session = await this.client.getSession(); // returns ECONNREFUSED exception

/*
wait for mysql to be ready and accepting connections
*/

const session = await this.client.getSession(); // returns connection pool queue timeout exception because the previous session hasn't been returned to the pool

如何确保中止的连接返回到池中?

【问题讨论】:

  • 应该说我不能手动调用.close()。抱歉,我无法编辑原始帖子。

标签: mysql node.js mysql-x-devapi


【解决方案1】:

这是一个错误,我鼓励您使用Connector for Node.js 类别在https://bugs.mysql.com/ 报告它。

如果getSession() 方法返回被拒绝的Promise(有或没有特定错误),唯一想到的解决方法是重新创建池。例如,类似:

const poolConfig = { pooling: { enabled: true, maxSize: 1, queueTimeout: 2000 } }
let pool = mysqlx.getClient(config, poolConfig)

let session = null

try {
  session = await pool.getSession()
} catch (err) {
  await pool.close()
  pool = mysqlx.getClient(config, poolConfig)

  session = await pool.getSession()
  // do something
}

这是一个丑陋的解决方案,可能很难硬塞到您的设计中,但至少,它可以让您享受连接池的其他好处。

免责声明:我是 MySQL X DevAPI Connector for Node.js 的首席开发人员

【讨论】:

  • 谢谢,我报告了这个错误。我已经有类似的解决方法,但我不会在 catch 中再次调用 getSession,因为错误很可能会再次发生。相反,我重新创建池,然后重新抛出错误,以便应用程序可以在一段时间后重试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2014-05-20
  • 2020-02-24
  • 2014-02-07
  • 2020-04-30
  • 1970-01-01
  • 2016-10-24
相关资源
最近更新 更多