Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node-mysql connection pooling

I am using node-mysql module (https://github.com/felixge/node-mysql) OR (http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/) .

Is this API handling connection pooling as well?

I mean with every user request I am calling Client.connect() to query the MySQL and to release the connection: Client.end().

Is this the right way, or should I connect/disconnect only once in a code.

I am learning from this document: https://github.com/felixge/node-mysql/blob/master/Readme.md

like image 997
Omer Aslam Avatar asked Jul 18 '11 09:07

Omer Aslam


People also ask

How do I create a MySQL connection pool in node JS?

Nodejs MySQL Integration: Pool Connectionsvar pool = mysql. createPool({ connectionLimit: 7, host: 'localhost', user: 'root', password: '', database: 'todoapp' });

Does MySQL support connection pooling?

X DevAPI supports connection pooling, which can reduce overhead for applications that open many connections to a MySQL Server. Connections are managed as a pool by a Client object.

What is MySQL connection pooling?

The MySQL Connection Pool operates on the client side to ensure that a MySQL client does not constantly connect to and disconnect from the MySQL server. It is designed to cache idle connections in the MySQL client for use by other users as they are needed.


2 Answers

Update: Feb 2013 - pool support has been added to node-mysql, see docs

Example using built-in pool:

var pool = require('mysql').createPool(opts);

pool.getConnection(function(err, conn) {
  conn.query('select 1+1', function(err, res) {
    conn.release();
  });
});

Pre 2013 solutions:

You can use node-pool or mysql-pool or use your own simple round-robin pool

function Pool(num_conns)
{
    this.pool = [];
    for(var i=0; i < num_conns; ++i)
        this.pool.push(createConnection()); // your new Client + auth
    this.last = 0;
}

Pool.prototype.get = function()
{
    var cli = this.pool[this.last];
    this.last++;
    if (this.last == this.pool.length) // cyclic increment
       this.last = 0;
    return cli;
}

now you can hope to have all queries callbacks to execute in 1 second:

var p = new Pool(16);
for (var i=0; i < 10; ++i)
{
    p.get().query('select sleep(1)', function() { console.log('ready'); } ); // server blocks for 1 second
}
like image 167
Andrey Sidorov Avatar answered Oct 19 '22 06:10

Andrey Sidorov


I believe same node-mysql package provides connection pooling. Have a look

var express   =    require("express");
var mysql     =    require('mysql');
var app       =    express();

var pool      =    mysql.createPool({
    connectionLimit : 100, //important
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'address_book',
    debug    :  false
});

function handle_database(req,res) {

    pool.getConnection(function(err,connection){
        if (err) {
          connection.release();
          res.json({"code" : 100, "status" : "Error in connection database"});
          return;
        }   

        console.log('connected as id ' + connection.threadId);

        connection.query("select * from user",function(err,rows){
            connection.release();
            if(!err) {
                res.json(rows);
            }           
        });

        connection.on('error', function(err) {      
              res.json({"code" : 100, "status" : "Error in connection database"});
              return;     
        });
  });
}

app.get("/",function(req,res){-
        handle_database(req,res);
});

app.listen(3000);

Read complete case study (with and without pool) : http://codeforgeek.com/2015/01/nodejs-mysql-tutorial/

like image 40
Shaikh Shahid Avatar answered Oct 19 '22 06:10

Shaikh Shahid