diff --git a/lib/connection-pool.js b/lib/connection-pool.js index 8679fa8..c4d8493 100755 --- a/lib/connection-pool.js +++ b/lib/connection-pool.js @@ -265,7 +265,7 @@ ConnectionPool.prototype.release = function(connection) { } }; -ConnectionPool.prototype.drain = function (callback) { +ConnectionPool.prototype.drain = async function (callback) { this.log('draining pool'); if (this.drained) {//pool has been drained if (callback) @@ -273,8 +273,10 @@ ConnectionPool.prototype.drain = function (callback) { return; } + // Flag as drained as this prevent others from acquiring new connections from the pool. this.drained = true; + // Remove all pending acquires. for (i = this.waiting.length - 1; i >= 0; i--) { var waiter = this.waiting[i]; @@ -282,7 +284,7 @@ ConnectionPool.prototype.drain = function (callback) { clearTimeout(waiter.timeout); } - this.waiting = null; + this.waiting.length = 0; var eventTotal = this.connections.length; var eventCount = 0; @@ -302,6 +304,14 @@ ConnectionPool.prototype.drain = function (callback) { clearTimeout(pooled.timeout); if (pooled.con) { + if (pooled.con.request) { + pooled.con.request.cancel(); + + // Make sure no requests are running before closing the connection. + while (pooled.con.request) { + await new Promise(done => setTimeout(done, 100)); + } + } pooled.con.on('end', ended); pooled.con.close(); } else { diff --git a/package.json b/package.json index 0e96a49..924c46f 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tedious-connection-pool", - "version": "1.0.5", + "version": "1.0.6", "description": "Connection Pool for tedious.", "main": "lib/connection-pool.js", "scripts": { @@ -30,8 +30,8 @@ } ], "license": "MIT", - "dependencies": { - "tedious": "^1.14.0" + "peerDependencies": { + "tedious": ">= 1.0.0" }, "devDependencies": { "mocha": "^3.0.2",