Skip to content

Commit

Permalink
fixing an issue with db pool links.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jun 1, 2017
1 parent 7ceb3e0 commit c3a2e5d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/connect.js
Expand Up @@ -11,7 +11,7 @@ function poolConnect(ctx, config, db) {
return config.promise((resolve, reject) => {
var pool = db.$pool;
if (!pool) {
pool = db.$pool = $npm.pools.getPool(ctx, config.pgp.pg, db);
pool = $npm.pools.acquirePool(ctx, config.pgp.pg, db);
}
pool.connect((err, client, done) => {
if (err) {
Expand Down
19 changes: 12 additions & 7 deletions lib/pools.js
Expand Up @@ -8,9 +8,9 @@ var $npm = {

var pools = {}; // global list of pools

function getPool(ctx, pg, db) {
function acquirePool(ctx, pg, db) {

var cp = new $npm.ConnectionParameters(ctx.cn),
var p, cp = new $npm.ConnectionParameters(ctx.cn),
ps = ctx.options.poolStrategy;

if ($npm.utils.isNull(ps)) {
Expand Down Expand Up @@ -44,20 +44,25 @@ function getPool(ctx, pg, db) {
}

if (poolName in pools) {
return pools[poolName].pool;
p = pools[poolName];
db.$pool = p.pool;
p.dbs.push(db);
return p.pool;
}

var p = new pg.Pool(ctx.cn);
p = new pg.Pool(ctx.cn);
p.on('error', onError);
pools[poolName] = {
pool: p, db
pool: p, dbs: [db]
};
return p;
}

function shutDown() {
for (var p in pools) {
pools[p].db.$pool = null;
pools[p].dbs.forEach(db => {
db.$pool = null;
});
pools[p].pool.removeListener('error', onError);
pools[p].pool.end(); // Mr. Dead Pool :)
}
Expand All @@ -82,7 +87,7 @@ function getAllPools() {
}

module.exports = {
getPool,
acquirePool,
getAllPools,
shutDown
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "6.0.4",
"version": "6.0.5",
"description": "Promises interface for PostgreSQL",
"main": "lib/index.js",
"typings": "typescript/pg-promise.d.ts",
Expand Down

0 comments on commit c3a2e5d

Please sign in to comment.