使用 node-postgres 库,为什么我要使用 Client 构造函数而不是 Pool 构造函数来建立数据库连接?
Using the node-postgres library, why would I ever use the Client constructor over the Pool constructor for establishing a database connection?
查看 node-postgres
documentation on connecting to a database server 看起来 Client
和 Pool
构造函数在功能上是等价的。
我的理解是,使用 Pool
构造函数为您提供与使用 Client
构造函数相同的功能,只是连接是从连接池建立的。
这不是总是令人向往吗?在什么条件下我会选择使用 Client
构造函数而不是 Pool
构造函数?
可以在这里找到一个相当不错的解释:https://gist.github.com/brianc/f906bacc17409203aee0。作为此 post 的一部分:
I would definitely use a single pool of clients throughout the application. node-postgres ships with a pool implementation that has always met my needs, but it's also fine to just use the require('pg').Client prototype and implement your own pool if you know what you're doing & have some custom requirements on the pool.
The drawback to using a pool for each piece of middleware or using multiple pools in your application is you need to control how many open clients you have connected to the backend, it's more code to maintain, and likely wont improve performance over using a single pool. If you find requests often waiting on available clients from the pool you can increase the size of the built in pool with pg.defaults.poolSize = 100 or something. I've set the default at 20 which is a sane default I think. If you have long running queries during web requests you probably have bigger problems than increasing your pool size is going to solve.
查看 node-postgres
documentation on connecting to a database server 看起来 Client
和 Pool
构造函数在功能上是等价的。
我的理解是,使用 Pool
构造函数为您提供与使用 Client
构造函数相同的功能,只是连接是从连接池建立的。
这不是总是令人向往吗?在什么条件下我会选择使用 Client
构造函数而不是 Pool
构造函数?
可以在这里找到一个相当不错的解释:https://gist.github.com/brianc/f906bacc17409203aee0。作为此 post 的一部分:
I would definitely use a single pool of clients throughout the application. node-postgres ships with a pool implementation that has always met my needs, but it's also fine to just use the require('pg').Client prototype and implement your own pool if you know what you're doing & have some custom requirements on the pool.
The drawback to using a pool for each piece of middleware or using multiple pools in your application is you need to control how many open clients you have connected to the backend, it's more code to maintain, and likely wont improve performance over using a single pool. If you find requests often waiting on available clients from the pool you can increase the size of the built in pool with pg.defaults.poolSize = 100 or something. I've set the default at 20 which is a sane default I think. If you have long running queries during web requests you probably have bigger problems than increasing your pool size is going to solve.