gpt4 book ai didi

mysql - fatal error 后的 Node JS Mysql 协议(protocol)入队

转载 作者:IT老高 更新时间:2023-10-28 23:11:09 33 4
gpt4 key购买 nike

错误是什么意思?

{ [Error: Cannot enqueue Query after fatal error.] code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }

此代码适用于我的测试文件:

function handleDisconnect() {
objConn = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
objConn.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err.code);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
}else{
console.log('Connected to db!');
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
objConn.on('error', function(err) {
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
}else{
throw err;
}
});
}

handleDisconnect();
megaLoop();

function megaLoop(){
objConn.query('SELECT u.`email` FROM `users` as u', function(err, rows) {
console.log(err);
console.log(rows);
});
setTimeout(megaLoop, 100);
}

但是当我在我的 Express 应用程序中使用该功能时,我得到了错误:

{ [Error: Cannot enqueue Query after fatal error.] code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }

为什么它在我的测试而不是我的应用中有效?

最佳答案

我在连接 MySQL 时遇到了类似的问题。我使用连接池解决了这个问题。其概念是仅在需要时从连接池中获取连接,并在使用后释放它。这样连接就不会处于不稳定状态。您可以获得实现细节here .

关于mysql - fatal error 后的 Node JS Mysql 协议(protocol)入队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549193/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com