所以现在我正在测试一些方法来更轻松地释放池的连接。通过更容易,我的意思是更容易阅读并意识到连接肯定会被释放。
我现在正在玩这个并且它似乎正在工作......但我很想知道这是不是从游泳池释放连接的可靠方法。
pool.getConnection(function (err, connection) {
if(err) {
return res.status(500).send('Error connecting to database');
}
runFunctionOne();
console.log(pool._freeConnections.indexOf(connection)); // returns -1 as expected
connection.release(); // Is releasing the connection here OK to do??
console.log(pool._freeConnections.indexOf(connection)); // returns 1 ... which makes me believe it doesnt release the function until after all queries have been done, no matter how many function with queries I run after the first one is invoked. But I don't understand how??
pool.on('release', function (connection) {
console.log('Connection %d released', connection.threadId);
}); // This doesn't log until after all of my functions(No matter how many I add) have been run.
function runFunctionOne() {
query = `stuff`
inserts = [stuff1, stuff2]
connection.query(query, inserts, function(err, results){
if(err) {
// handle error
} else {
runFunctionTwo(results.key); // goes to the next function
}
});
});
function runFunctionTwo(key) { ...
....
});
我在调用第一个函数后运行了多达10个函数来测试它。我已经在客户端连接中查看了我的数据库,它似乎仍在合并。但是在我开始使用这种释放连接的方式之前,如果这样做是否安全,我肯定会有一个肯定的答案。"去"在将来。
如果可以通过这种方式释放连接,有人可以向我解释原因吗?我不明白这是如何运作的。
提前致谢!!