错误:已从 nodejs 应用程序排队握手后,无法将握手排队



我每 5 秒查询一次表并显示给用户。我正在使用mysql 8。应用程序是 NodeJS。

var con = mysql.createConnection({
host: "localhost",
user: "root",
password: xxxxxx,
database: "test"
});

app.get('/users', (req, res) => {
con.connect(function(err) {
if (err) throw err;
con.query(" select * fron table1; ",function (err, result, fields) {
if (err) throw err;
console.log(result);
if (result.length > 0) {
res.send(result);
} else {
res.send({});
}
});
});

当我再次调用该功能时,它的给出错误。 这是因为我看到我没有关闭连接。如果我尝试关闭连接,那么它也不起作用。 我尝试了con.close((。 有没有办法让我骑这个

我认为使用池化是最好的方法,我已经通过使用连接池并删除.connect().end()来修复此错误,使用.release相反,例如:

var pool        = mysql.createPool({
connectionLimit : 10, // default = 10
host            : 'localhost',
user            : 'root',
password        : '******',
database        : 'test'
});
router.get('/users', function (req, res) {
pool.getConnection(function (err, connection) {
connection.query("SELECT * FROM table1", function (err, rows) {
connection.release();
if (err) throw err;
console.log(rows.length);
res.send(JSON.stringify(rows));
});
});
});

相关内容

最新更新