database - node-mysql connection pooling -



database - node-mysql connection pooling -

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

is api handling connection pooling well?

i mean every user request calling client.connect() query mysql , release connection: client.end().

is right way, or should connect/disconnect 1 time in code.

i learning document: https://github.com/felixge/node-mysql/blob/master/readme.md

update: feb 2013 - pool back upwards has been added 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 utilize node-pool or mysql-pool or utilize own simple round-robin pool

function pool(num_conns) { this.pool = []; for(var i=0; < num_conns; ++i) this.pool.push(createconnection()); // 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; homecoming cli; }

now can hope have queries callbacks execute in 1 second:

var p = new pool(16); (var i=0; < 10; ++i) { p.get().query('select sleep(1)', function() { console.log('ready'); } ); // server blocks 1 sec }

mysql database node.js connection-pooling

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -