节点js app中数据库连接池处理的最佳实践?

时间:2022-12-06 20:17:27

I'm referring to node-postgres package below, but I guess this question is rather generic.

我指的是下面的node-postgres包,但我想这个问题很通用。

There is this trivial example where you 1) acquire (connect) a connection (client) from the pool in the top level http request handler, 2) do all business inside of that handler and 3) release it back to the pool after you're done.

有一个简单的例子,你1)从*http请求处理程序中的池中获取(连接)连接(客户端),2)在该处理程序内完成所有业务,3)在你'之后将它释放回池中重做。

I guess it works fine for that example, but as soon as your app becomes somewhat bigger this becomes painfull soon.

我想这个例子工作得很好,但是一旦你的应用程序变得更大,这很快就会变得痛苦。

I'm thinking of these two options, but I'm not quite sure...

我正在考虑这两个选项,但我不太确定......

  1. do the "get client + work + release client" approach everywhere I need to talk to db.

    在我需要与db交谈的地方做“获取客户端+工作+发布客户端”的方法。

    This seems like a good choice, but will it not lead to eating up more than one connection/client per the top http request (there are parallel async db calls in many places in my project)?

    这似乎是一个不错的选择,但它不会导致每个*http请求占用多个连接/客户端(在我的项目中的许多地方都有并行异步数据库调用)?

  2. try to assign a globaly shared reference to one client/connection accessible via require()

    尝试将一个globaly共享引用分配给一个可通过require()访问的客户端/连接

    Is this a good idea and actually reasonably doable? Is it possible to nicely handle the "back to the pool release" in all ugly cases (errors in parallel async stuff for example)?

    这是一个好主意,实际上是否合理可行?是否有可能在所有丑陋的情况下很好地处理“返回池发布”(例如并行异步内容中的错误)?

Thank you.

3 个解决方案

#1


1  

Well, I lost some time trying to figure that out. At the end, after some consideration and influenced by John Papa's code I decided use a database module like this:

好吧,我失去了一些时间试图解决这个问题。最后,经过一些考虑并受到John Papa代码的影响,我决定使用这样的数据库模块:

var Q = require('q');
var MongoClient = require('mongodb').MongoClient;

module.exports.getDb = getDb;

var db = null;

function getDb() {
  return Q.promise(theDb);

  function theDb(resolve, reject, notify) {
    if (db) {
      resolve(db);
    } else {
      MongoClient.connect(mongourl, mongoOptions, function(err, theDb) {            
          resolve(db);
        }
      });
    }
  }
}

So, when I need to perform a query:

所以,当我需要执行查询时:

getDb().then(function(db) {

    //performe query here

});

At least for Mongodb this is good practice as seen here.

至少对于Mongodb来说,这是一个很好的做法,如此处所示。

#2


0  

The best advise would depend on the type of database and the basic framework that represents the database.

最好的建议取决于数据库的类型和代表数据库的基本框架。

In case of Postgres, the basic framework/driver is node-postgres, which has embedded support for connection pool. That support is however low-level.

对于Postgres,基本框架/驱动程序是node-postgres,它嵌入了对连接池的支持。然而,这种支持是低水平的。

For high-level access see pg-promise, which provides automatic connection management, support for tasks, transactions and much more.

对于高级访问,请参阅pg-promise,它提供自动连接管理,对任务,事务的支持等等。

#3


0  

Here is what has worked well for me.

这对我来说效果很好。

var pg = require('pg');
var config = { pg : 'postgres://localhost/postgres' };

pg.connect(config.pg, function(err, client, done) {
  client.query('SELECT version();', function (err, results) {
    done();

    //do something with results.rows
  });
});

#1


1  

Well, I lost some time trying to figure that out. At the end, after some consideration and influenced by John Papa's code I decided use a database module like this:

好吧,我失去了一些时间试图解决这个问题。最后,经过一些考虑并受到John Papa代码的影响,我决定使用这样的数据库模块:

var Q = require('q');
var MongoClient = require('mongodb').MongoClient;

module.exports.getDb = getDb;

var db = null;

function getDb() {
  return Q.promise(theDb);

  function theDb(resolve, reject, notify) {
    if (db) {
      resolve(db);
    } else {
      MongoClient.connect(mongourl, mongoOptions, function(err, theDb) {            
          resolve(db);
        }
      });
    }
  }
}

So, when I need to perform a query:

所以,当我需要执行查询时:

getDb().then(function(db) {

    //performe query here

});

At least for Mongodb this is good practice as seen here.

至少对于Mongodb来说,这是一个很好的做法,如此处所示。

#2


0  

The best advise would depend on the type of database and the basic framework that represents the database.

最好的建议取决于数据库的类型和代表数据库的基本框架。

In case of Postgres, the basic framework/driver is node-postgres, which has embedded support for connection pool. That support is however low-level.

对于Postgres,基本框架/驱动程序是node-postgres,它嵌入了对连接池的支持。然而,这种支持是低水平的。

For high-level access see pg-promise, which provides automatic connection management, support for tasks, transactions and much more.

对于高级访问,请参阅pg-promise,它提供自动连接管理,对任务,事务的支持等等。

#3


0  

Here is what has worked well for me.

这对我来说效果很好。

var pg = require('pg');
var config = { pg : 'postgres://localhost/postgres' };

pg.connect(config.pg, function(err, client, done) {
  client.query('SELECT version();', function (err, results) {
    done();

    //do something with results.rows
  });
});