如何在node-postgres中修复client.release()末尾的无限承诺循环?



这是我尝试使用node-postgres实现的代码:

return pool.connect().then((client) => {
//  if (err) {
//      throw err;
//  }
let updateTable = [];
console.log('ABOUT TO RUN QUERY');
// updateTable.executeQuery()
return client.query(sqlString, updateTable, (error, result) => {
console.log('RUNNING QUERY');
if (error) {
throw error;
}
console.log('RUNNING QUERY2');
// code
console.log('RUNNING QUERY3');
for (let i = 0; i < result.rows.length; i++) {
console.log('RUNNING QUERY4');
let row = result.rows[i];
// process data
}
console.log('RUNNING QUERY5');
// write to file
console.log('RUNNING QUERY6');
return client.release();
})
.then(() => {
console.log('CLIENT RELEASED');
if (!fileOnly) {
if (optionNode != null) {
console.log('ABOUT TO RUN QUERY #2');
// statsTable.executeQuery()
let statResults = client.query(withStatsString, statsTable, (err, res) => {
if (err) {
console.log(err);
return err;
}
return client.release();
});
//}
}
}
return pool.end();
})
.then(() => {
return reportObject;
})
.catch(e => {
throw e;
});
})
.catch((e) => {
console.log(e);
return reportObject;
});

当我运行此代码时,我可以看到:

RUNNING QUERY
RUNNING QUERY2
RUNNING QUERY3
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY4
RUNNING QUERY5
RUNNING QUERY6

但是,它永远不会到达释放客户端的then。我会在结束Promise之前从字面上打印出来,但会无限挂起并且永远不会解决。如何修复我的承诺链?

编辑:我能够修复client.query,但在index.js,我的程序在完成时挂起。这是代码:

ReportUtil.sendInReport('Monthly_2017_01', JSON.parse(reportRequest), 100, null)
.then(result => {
console.log(result.status);
console.log(result.header);
console.log(result.data);
return Promise.resolve();
}).finally(() => {});

在此代码之后,它只是挂起,程序永远不会结束。我如何逃脱这个承诺链?

根据发布回调的文档:

您必须调用 releaseCallback 或 client.release(它指向 释放回调(,当您完成客户端时。如果你 忘记释放客户端,然后您的应用程序将很快 排气可用,池中的空闲客户端以及所有进一步的调用 pool.connect 将超时并显示错误或无限期挂起,如果您 将连接超时铣床配置为 0。

尽量不要返回client.release(就像它是一个承诺 - 它不是(,而只是像这样在承诺链中将其称为较低的位置:

return pool.connect().then((client) => {
let updateTable = [];
console.log('ABOUT TO RUN QUERY');
// updateTable.executeQuery()
return client.query(sqlString, updateTable)
.then(result => {
console.log('RUNNING QUERY');
console.log('RUNNING QUERY2');
// code
console.log('RUNNING QUERY3');
for (let i = 0; i < result.rows.length; i++) {
console.log('RUNNING QUERY4');
let row = result.rows[i];
// process data
}
console.log('RUNNING QUERY5');
// write to file
console.log('RUNNING QUERY6');
})
.then(() => {
if (!fileOnly) {
if (optionNode != null) {
console.log('ABOUT TO RUN QUERY #2');
// statsTable.executeQuery()
let statResults = client.query(withStatsString, statsTable)
.then(res => {
});
}
}
client.release();
console.log('CLIENT RELEASED');
return pool.end();
})
.then(() => {
return reportObject;
})
.catch(e => {
client.release();
throw e;
});
})
.catch((e) => {
console.log(e);
return reportObject;
});

对于使用承诺链退出index.js

ReportUtil.sendInReport('Monthly_2017_01', JSON.parse(reportRequest), 100, null)
.then(result => {
console.log(result.status);
console.log(result.header);
console.log(result.data);
})
.catch(e => {
console.log(e);
})
.finally(() => {
process.exit();
});

最新更新