Nodejs 使用连接池访问postgresSql
记录一次使用nodejs pool连接池访问postgresql的一次经历,还遇到了无法释放连接池的bug
代码段
const pool = new Pool(config)
/**
* 异步请求函数,查询数据库
* @returns {Promise<any>}
*/
const doQueryCategoryInfo = function () {
var p = new Promise(function (resolve, reject) {
//做一些异步操作
pool.connect().then(client => {
// insert 数据
client.query("Select * FROM t_category_info").then(res => {
client.release() //释放连接池
var value = res.rows
resolve(value)
return res
})
})
});
return p;
}
- 使用Promise函数了
- 之前遇到的bug就是没有加上*
***client.release() //释放连接池***
*,导致无法师父连接池,最后查了官方文档,发现用错了 ,官方文档地址: https://node-postgres.com/api/result