Commit de8d1285 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Reconnect comments server if MySQL connection lost.

This should take care of the occasional problem in comments server
when it from time-to-time goes down because of a slight glitch in
MySQL connection.
parent 6e02337b
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -20,12 +20,21 @@ var ConnectionPool = {
     */
    get: function(name, cfg) {
        if (!this.connections[name]) {
            this.connections[name] = mysql.createConnection(cfg);
            this.initConnection(name, cfg);
        }

        return this.connections[name];
    },

    initConnection: function(name, cfg) {
        this.connections[name] = mysql.createConnection(cfg);
        this.connections[name].on("error", function(err) {
            if (err.fatal && err.code === 'PROTOCOL_CONNECTION_LOST') {
                console.error("MySQL Connection lost, reconnecting: "+err.stack);
                this.initConnection(name, cfg);
            }
        }.bind(this));
    }
};

module.exports = ConnectionPool;