Search code examples
mysqlnode.jsexpresssequelize.jsnode-mysql

how to implement clustering in Sequelize?


I have two mysql server both are (MASTER,MASTER). how could we implement clustering in sequelize. if one of sql server has stopped then all request goes to other mysql server without restarting node server.


Solution

  • They haven't implemented clustering feature or fallback. I have found a way around for the same.

    var Sequelize = require("sequelize");
    
    sequelize.connectionManager.connect = function(){
        return new Promise(function(resolve,reject){
           // create your connection and return its instance in promise
           resolve(connection);
        });
    }
    
    sequelize.connectionManager.disconnect = function(connection){
       // to disconnect the connection
       if (!connection._protocol._ended) {
           connection.release()
    }
    return Promise.resolve();
    
    }
    

    I can share code if above code is not enough.