Skip to content

Error: Connection lost: The server closed the connection. #528

Closed
@apparatusdeus

Description

@apparatusdeus

I keep leaving node running over night and when I come back in the morning the instance has thrown an exception and ended.

Error: Connection lost: The server closed the connection.
    at Protocol.end (/node/node_modules/mysql/lib/protocol/Protocol.js:72:13)
    at Socket.onend (_stream_readable.js:483:10)
    at Socket.g (events.js:175:14)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:415:13)

I am unsure why this is occurring. I created a connection pool under the impression that unused connections that terminate would be handled and reconnected when needed again. Is this untrue or am I not closing my connection correctly? Do I have to explicitly handle the disconnection even on a connection pool?

Here is a portion of my code:

var mysqlPool = mysql.createPool({
    host     : '*HOST*',
    user     : '*USERNAME*',
    password : '*PASSWORD*',
    database : '*DATABASE*'
});

var statues = {};
mysqlPool.getConnection(function(err, connection) {
    if(err) throw err;

    connection.query('SELECT * FROM table', function(err, rows) {
        if(err) {
            console.error(err);
            process.exit();
            return;
        }
        for(row in rows) {
            statues[rows[row]['id']] = rows[row]['value'];
        }

        /* Do other stuff - No other queries */
        connection.end(); // Didn't have this originally. Is it required?
    });

    connection.end();
});

If I called another query within the callback of the first query would I also need to call connection end in its callback or is it only required in the getConnection callback.

The instance very rarely hits the database as it only needs it for specific tasks so I'm guessing the connection is being terminated because its its not in use.

Sorry if this all seems a little obvious to anyone I'm still getting use to the whole Asynchronous coding style (I use PHP normally).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions