當我關閉在數據庫連接的Node.js我收到此錯誤如何關閉node.js中的數據庫連接?
Cannot enqueue Query after invoking quit
。
這裏是我的代碼
socket.on('adminConnect', function (email) {
connection = mysql.createConnection(db_config); // db_config has all details of database
connection.connect(); // no problem in connection
connection.query("SELECT id FROM user WHERE email = '" + email + "' ", function (err, results, fields) {
if (err) throw err;
if (results[0]) {
// Some code
connection.end(); // its giving error "Cannot enqueue Query after invoking quit."
} else {
console.log("no id");
}
});
});
通常情況下,你不希望創建爲每個reques一個新的連接噸。保持連接以獲得更多額外的查詢。 – Zeta
好吧,我會做到這一點。但我的問題是在哪裏結束連接? – Vardan
@用戶您的觀點不是。爲什麼要關閉可以重用的連接?連接很便宜,但打開它們需要一次或三次。 –