循环中的查询 - 连接丢失 - 节点JS

时间:2017-06-25 21:22:27

标签: mysql node.js loops connection

我试图在while循环中执行一堆非常大的SELECT语句(每个语句都需要几分钟才能完成执行)。

var con = mysql.createConnection(
{

    host: "host_name",
    user: "user_name",
    password: "password",
    database: "database_name"

});

con.connect();

while(arg1 < arg2)
{

    var query = SELECT * FROM table WHERE value=arg1;
    con.query(query, function (err, result) 
    {
        //Rest of code
    });
    arg = arg + 1;

}
con.end();

起初,它正在工作,但很快,经过一段时间后,连接似乎就丢失了。

Connection lost: The server closed the connection.
    at Protocol.end (C:\Users\X\node_modules\mysql\lib\protocol\Protocol.js:109:13)
    at Socket.<anonymous> (C:\Users\X\node_modules\mysql\lib\Connection.js:109:28)
    at emitNone (events.js:110:20)
    at Socket.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1047:12)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)

在阅读Stack上的一些类似问题之后,由于Github上的Mysql页面,我明白了原因:

  

由于网络问题,服务器计时,服务器重启或崩溃,您可能会丢失与MySQL服务器的连接。

因此,为了处理可能的断开连接,我尝试使用此功能,遵循上一篇文章的工作:

function handleDisconnect() {

    connection = mysql.createConnection(db_config); 

    connection.connect(function(err) {              

        if(err) {    

        console.log('error when connecting to db:', err);
        setTimeout(handleDisconnect, 2000); 

        }                                     
    });                                     

    connection.on('error', function(err) {

        console.log('db error', err);
        if(err.code === 'PROTOCOL_CONNECTION_LOST') {

            handleDisconnect();                         
        } 
        else 
        {                                     
            throw err;                                  
        }
    });
}

在以下代码中:

while(arg1 < arg2)
{

    handleDisconnect();       
    var query = SELECT * FROM table WHERE value=arg1;
    connection.query(query, function (err, result) 
    {
        //Rest of code
    });
    arg = arg + 1;

}

但我仍然收到以下错误消息:

error when connecting to db: { Error: Connection lost: The server closed the connection.
    at Protocol.end (C:\Users\X\node_modules\mysql\lib\protocol\Protocol.js:109:13)
    at Socket.<anonymous> (C:\Users\X\node_modules\mysql\lib\Connection.js:109:28)
    at emitNone (events.js:110:20)
    at Socket.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1047:12)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)
    --------------------
    at Protocol._enqueue (C:\Users\X\node_modules\mysql\lib\protocol\Protocol.js:141:48)
    at Protocol.handshake (C:\Users\X\node_modules\mysql\lib\protocol\Protocol.js:52:41)
    at Connection.connect (C:\Users\X\node_modules\mysql\lib\Connection.js:130:18)
    at handleDisconnect (C:\Users\X\connection.js:182:13)
    at Object.<anonymous> (C:\Users\X\connection.js:105:2)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
C:\Users\X\connection.js:213
                if (err) throw err;
                         ^

Error: Connection lost: The server closed the connection.
    at Protocol.end (C:\Users\X\node_modules\mysql\lib\protocol\Protocol.js:109:13)
    at Socket.<anonymous> (C:\Users\X\node_modules\mysql\lib\Connection.js:109:28)
    at emitNone (events.js:110:20)
    at Socket.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1047:12)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)
    --------------------
    at Protocol._enqueue (C:\Users\X\node_modules\mysql\lib\protocol\Protocol.js:141:48)
    at Protocol.handshake (C:\Users\X\node_modules\mysql\lib\protocol\Protocol.js:52:41)
    at Connection.connect (C:\Users\X\node_modules\mysql\lib\Connection.js:130:18)
    at handleDisconnect (C:\Users\X\connection.js:182:13)
    at Object.<anonymous> (C:\Users\X\connection.js:105:2)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)

有什么东西我没有得到或有什么我应该知道成功管理断开连接?我仍然遇到Node JS的问题,理解如何处理同步和异步函数的概念,也许我的问题来自这里。

0 个答案:

没有答案