0

我想知道在使用 node express 服务器运行 pg-co 时,是否在每个客户端连接上都连接到数据库?还是保持全球联系?

参考:https ://github.com/basicdays/co-pg

换句话说,这是我的节点模块:

module.exports.set = function(app, worker_id) {

   var ms = require("mslibmod"),
      co = require('co'),
      async = require('async'),
      pgconn = require('pgconn').create(),
      pg = require('co-pg')(require('pg')),
      exec = require('co-exec'),
      thunkify = require('thunkify');

    app.get('/getLines', function(req, res, next) {
        var id = req.param('id');

        co(function*() {
            try {
                var pgHandle = yield pg.connect_(pgconn.dbserver('galaxy'));
                var statement = 'select business_name FROM business_info WHERE business_id = ' + '1016';
                var result = yield pgHandle[0].query_(statement);
                pgHandle[1]();  // pg queue done
                if (result.rows[0] == undefined){}
                ms.log(result.rows[0].business_name);

            } catch (e) {
                console.error('Panic: ' + e.toString());
            }
        })();

        res.send({});
    });

};

注意我如何运行命令:

var pgHandle = yield pg.connect_(pgconn.dbserver('galaxy'));

每次连接新的快速客户端时,这是正确的方法吗?不会导致内存泄漏?

4

0 回答 0