我正在使用节点pg模块,并且尝试在初始连接结束后使用相同的客户端连接到postgres,但这不起作用。
通过查看pg_stat_activity表,我可以看到连接已关闭。但即使在该client.connect失败之后。
以下代码每5秒调用一次查询,每次都建立一个新连接。我不想为此而建立连接,因此每次都试图获得一个新的连接。
const client = new Client(pg_config);
function do_it(){
try {
console.log('connecting');
client
.connect()
.then(() => {
client
.query('SELECT do_proc()')
.then(() => {
client
.end()
.then(() => {
console.log('connection ended');
setTimeout(do_it, 5000);
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
} catch(err) {
console.log(err);
}
}
do_it();