node.js的generic-pool與mysql結合,mysql連接池

var generic_pool = require('generic-pool');



var pool = generic_pool.Pool({

    name: 'mysql',

    max: 10,

    create: function(callback) {

        var Client = require('mysql').createConnection({

            host:'127.0.0.1',

            user:'root',

            password:'123456',

            database: 'weibo_gs'

        });



        callback(null,Client);

    },

    destroy: function(db) {

        db.disconnect();

    }

});



pool.acquire(function(err, client) {



    if (err) {

        // handle error - this is generally the err from your

        // factory.create function  

    }

    else {

        client.query("select * from gs_scrapy", [], function(err,data) {

            console.log(data);

            // return object back to pool

            pool.release(client);

        });

    }

});

發佈了112 篇原創文章 · 獲贊 37 · 訪問量 68萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章