Error: Cannot enqueue Query after fatal error. Expressjs. Mysql
我将我的Express js连接到多个数据库。每次我启动我的应用程序时都可以使用。但是,一旦我与数据库的连接失效,该连接就会返回错误代码
问题是,每当我的快速应用程序遇到
这是我连接数据库的示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | var mysql = require('mysql'); var mysqlConn // mysqlConn.connect(); function handleDisconnect() { mysqlConn = mysql.createConnection({ host: 'aws_instance***', user: '******', password: '*****', database: 'my_db', multipleStatements: true }); mysqlConn.connect(function (err) { if (err) { console.log('ERROR CONNECT admin:', err.code + '--' + err.address); setTimeout(handleDisconnect, 2000); } else { console.log('Connected to DB') } }); mysqlConn.on('error', function (err) { console.log('ERROR admin', err.code + '--' + err.address); if (err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually console.log("Connection to db lost!") handleDisconnect(); // lost due to either server restart, or a } else { console.log(err) // connnection idle timeout (the wait_timeout throw err; // server variable configures this) } }); } handleDisconnect(); module.exports = { mysqlConn: mysqlConn, }; |
然后这是我的输出日志,如服务器日志中所示。
1 2 3 4 5 6 7 8 9 10 11 12 | ERROR db PROTOCOL_CONNECTION_LOST--undefined Connection to db lost! Connected to db OPTIONS /verify-token/ 200 0.285 ms - 4 Error: Cannot enqueue Query after fatal error. POST /verify-token/ 500 1.332 ms - 95 OPTIONS /auth/login 200 0.793 ms - 4 Error: Cannot enqueue Query after fatal error. POST /login 500 1.564 ms - 58 OPTIONS /login 200 0.687 ms - 4 Error: Cannot enqueue Query after fatal error. POST /login 500 1.467 ms - 58 |
尽管有变通办法,但它们显然并不适合所有人。文档中的建议是使用连接池,而不是手动管理单个连接。