【发布时间】:2022-10-01 09:07:46
【问题描述】:
我们在node-postgres 文档中有以下内容:
// number of milliseconds to wait before timing out when connecting a new client
// by default this is 0 which means no timeout
connectionTimeoutMillis?: int,然后,稍后在同一个文档中:
完成客户端后,您必须调用 releaseCallback 或 client.release(指向 releaseCallback)。如果您忘记释放客户端,那么您的应用程序将很快耗尽可用的客户端,池中的空闲客户端以及对 pool.connect 的所有进一步调用将超时并出现错误或如果您将 connectionTimeoutMillis 配置为 0,则无限期挂起.
所以,我期待如果我将
connectionTimeoutMillis设置为1000,那么在空闲1s后,它应该会自动释放连接,即使我不调用client.release()。但是运行下面的代码在 PostgreSQL 上永远处于空闲状态:
// test.js // PostgreSQL v14.2 // Node.js v16.15.1 // node-postgres (pg) v8.7.3 const { Pool } = require(\'pg\') const pool = new Pool({ user: \'postgres\', password: \'postgres\', host: `localhost`, port: 5432, database: \'app_dev\', max: 10, connectionTimeoutMillis: 1000, idleTimeoutMillis: 1000 }) ;(async function() { const client = await pool.connect() const {rows} = await client.query(\'SELECT NOW()\') console.log(rows[0]) })()我错过了什么吗?
标签: node.js postgresql node-postgres