在致命错误之后节点JS Mysql协议ENQUEUE

时间:2015-07-21 20:40:24

标签: mysql node.js

错误是什么意思?

{ [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 App中使用该功能时,我收到错误:

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

为什么它在我的测试中起作用而不是我的应用程序?

2 个答案:

答案 0 :(得分:4)

我认为问题是express-sql-session。我现在遇到同样的问题。我认为有人会在这篇文章中发表意见:NodeJS running on MAC, and have an error occurred when deploying on centOS

也请查看:https://github.com/felixge/node-mysql/issues/1166

答案 1 :(得分:4)

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

相关问题