在node-mysql中建立池连接

时间:2013-06-29 05:30:47

标签: mysql node.js

如何在node-mysql中建立池连接? 任何人都可以提供语法吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

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;
}