Search code examples
mysqlsails.jsconnection-pooling

Connection pooling with Sailsjs


Since there is not documentation about how to create connection poolings with Sailsjs, I've being searching a little bit about it and I've found that sails-mysql adapter do have any pooling configuration in it but is related to tests:

{
    host: process.env.WATERLINE_ADAPTER_TESTS_HOST || 'localhost',
    // (...)
    database: process.env.WATERLINE_ADAPTER_TESTS_DATABASE || 'sails_mysql',
    pool: true,
    connectionLimit: 10,
    waitForConnections: true
}

So I've tried to include pool: true to my connections.js file in Sails:

someMysqlServer: {
    adapter: 'sails-mysql',
    host: '127.0.0.1',
    user: 'root',
    password: '',
    pool: true,
    database: 'mydatabase'
},

Server still runs and I recover data from the database but I'm not sure at all about if I have set connection throw a pooling service. Is there any way to check this feature?

Thanks.


Solution

  • Everything is there in codes

    you can see the codes of sails-mysql(node modules) and you will find the solution for your doubts for sure. in sails-mysql module the main js is node-modules/sails-mysql/lib/adapter.js (according to node-modules/sails-mysql/package.json). so in adapter.js file you can see..

    /*--codes--*/
    var _spawnConnection = require('./connections/spawn');
    /*--codes--*/
    defaults: {
      pool: true,
      connectionLimit: 5,
      waitForConnections: true
    },
    /*--codes--*/
    

    which is setting for to use pool by default with connectionLimit 5.

    Now go to the file node-modules/sails-mysql/lib/connections/spawn.js there you see this snippet.

    /*--codes--*/
    /*--codes--*/
      if (connectionObject.connection.pool) {
        connectionObject.connection.pool.getConnection(function (err, conn) {
          afterwards(err, conn);
        });
        return;
      }
    
      // Use a new connection each time
      var conn = mysql.createConnection(connectionObject.config);
      conn.connect(function (err) {
        afterwards(err, conn);
      });
      return;
    /*--codes--*/
    /*--codes--*/
    

    here it check if the adapter is set to use pool or not if it is pool:true then a connection is spawned from pool other wise a lone connection is used without pooling...

    Now coming to your doubt....in your config/connections.js if u define an adapter like

    mysqlServerMaster: {
        adapter: 'sails-mysql',
        host: 'localhost',
        user: 'root',
        password: 'root',
        pool: false,
        database: 'payments_db'
    },
    

    then you are using mysql without pooling.

    and else If this setting you use...

    mysqlServerMaster: {
        adapter: 'sails-mysql',
        host: 'localhost',
        user: 'root',
        password: 'root',
        pool: true,
        connectionLimit: 10,
        database: 'payments_db'
    },
    

    then you are having a mysql connection pool with connectioLimit ===10 for sure. I hope you got it now. https://www.npmjs.com/package/sails-mysql