diff --git a/README.md b/README.md index d82e9fe..a3f7a71 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,9 @@ The following method is added to the Tedious [Connection](http://pekim.github.co ### Connection.release() Release the connect back to the pool to be used again +## Version 0.3.7 Changes +* bug fix only + ## Version 0.3.6 Changes * bug fix only diff --git a/lib/connection-pool.js b/lib/connection-pool.js index fb2dd75..0d553c3 100644 --- a/lib/connection-pool.js +++ b/lib/connection-pool.js @@ -59,6 +59,7 @@ function createConnection(pooled) { return; var self = this; + var endHandler; this.log('creating connection: ' + cid); var connection = new Connection(this.connectionConfig); @@ -79,18 +80,18 @@ function createConnection(pooled) { var handleError = function(err) { self.log('connection closing because of error'); - self.emit('error', err); + + connection.removeListener('end', endHandler); + connection.close(); pooled.status = RETRY; pooled.con = undefined; - if (pooled.timeout) { + if (pooled.timeout) clearTimeout(pooled.timeout); - pooled.timeout = undefined; - } - connection.removeAllListeners('end'); - connection.close(); - setTimeout(createConnection.bind(self, pooled), self.retryDelay); + pooled.timeout = setTimeout(createConnection.bind(self, pooled), self.retryDelay); + + self.emit('error', err); }; connection.on('connect', function (err) { @@ -115,7 +116,7 @@ function createConnection(pooled) { connection.on('error', handleError); - connection.on('end', function () { + var endHandler = function () { self.log('connection ended: ' + pooled.id); if (self.drained) //pool has been drained return; @@ -127,7 +128,9 @@ function createConnection(pooled) { return; } } - }); + }; + + connection.on('end', endHandler); } function fill() { @@ -270,21 +273,6 @@ ConnectionPool.prototype.drain = function (callback) { this.drained = true; - var eventTotal = this.connections.length + this.waiting.length; - var eventCount = 0; - - if (eventTotal === 0) { - if (callback) - callback(); - return; - } - - var ended = function() { - if (++eventCount === eventTotal) - if (callback) - callback(); - }; - for (i = this.waiting.length - 1; i >= 0; i--) { var waiter = this.waiting[i]; @@ -294,14 +282,29 @@ ConnectionPool.prototype.drain = function (callback) { this.waiting = null; + var eventTotal = this.connections.length; + var eventCount = 0; + + if (eventTotal === 0 && callback) + callback(); + + var ended = function() { + if (++eventCount === eventTotal && callback) + callback(); + }; + for (var i = this.connections.length - 1; i >= 0; i--) { var pooled = this.connections[i]; - pooled.con.on('end', ended); if (pooled.timeout) clearTimeout(pooled.timeout); - pooled.con.close(); + if (pooled.con) { + pooled.con.on('end', ended); + pooled.con.close(); + } else { + ended(); + } } this.connections = null; diff --git a/package.json b/package.json index 343f7f2..6429eeb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tedious-connection-pool", - "version": "0.3.6", + "version": "0.3.7", "description": "Connection Pool for tedious.", "main": "lib/connection-pool.js", "scripts": { diff --git a/test/connection-pool.test.js b/test/connection-pool.test.js index 8753556..f1f2a8d 100644 --- a/test/connection-pool.test.js +++ b/test/connection-pool.test.js @@ -32,7 +32,6 @@ ALTER LOGIN test DISABLE */ describe('ConnectionPool', function () { - it('min', function (done) { this.timeout(10000); @@ -181,8 +180,12 @@ describe('ConnectionPool', function () { }); it('pool error event', function (done) { - var poolConfig = {min: 2, max: 5}; + this.timeout(10000); + var poolConfig = {min: 3}; var pool = new ConnectionPool(poolConfig, {}); + + pool.acquire(function() { }); + pool.on('error', function(err) { assert(!!err); pool.drain(done); @@ -334,4 +337,18 @@ describe('ConnectionPool', function () { })); }); }); + + it('drain', function (done) { + this.timeout(10000); + + var poolConfig = {min: 3}; + var pool = new ConnectionPool(poolConfig, {}); + + pool.acquire(function() { }); + + setTimeout(function() { + assert.equal(pool.connections.length, poolConfig.min); + pool.drain(done); + }, 4); + }); });