我使用的是@mysql/xdevapi npm包(版本8.0.22)和本地安装的mysql-8.0.15-winx64。
我已将pooling设置为enabled,并尝试从客户端检索会话。如果我在mysql准备好之前这样做,那么我会得到一个ECONNREFUSED异常,这是意料之中的,但连接似乎永远不会被释放。如果池大小为1,则所有后续尝试均为getSession
异常是从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如何确保中止的连接返回到池中?
发布于 2020-12-09 17:51:00
这是一个错误,我鼓励您使用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
}这是一个丑陋的解决方案,可能很难硬塞进你的设计中,但至少,它让你享受到了连接池的其他好处。
免责声明:我是Node.js版MySQL X DevAPI连接器的首席开发人员
https://stackoverflow.com/questions/65188004
复制相似问题