diff --git a/Changes.md b/Changes.md index dae71c9c2..6be620188 100644 --- a/Changes.md +++ b/Changes.md @@ -8,6 +8,7 @@ you spot any mistakes. * Add `POOL_CLOSED` code to "Pool is closed." error * Add `POOL_CONNLIMIT` code to "No connections available." error #1332 +* Bind underlying connections in pool to same domain as pool #1242 * Bind underlying socket to same domain as connection #1243 * Fix edge cases constructing long stack traces #1387 * Fix Query stream to emit close after ending #1349 #1350 diff --git a/lib/PoolConnection.js b/lib/PoolConnection.js index dc47214fd..e4083f7b1 100644 --- a/lib/PoolConnection.js +++ b/lib/PoolConnection.js @@ -1,5 +1,6 @@ -var inherits = require('util').inherits; +var inherits = require('util').inherits; var Connection = require('./Connection'); +var Events = require('events'); module.exports = PoolConnection; inherits(PoolConnection, Connection); @@ -8,6 +9,17 @@ function PoolConnection(pool, options) { Connection.call(this, options); this._pool = pool; + // Bind connection to pool domain + if (Events.usingDomains) { + if (this.domain) { + this.domain.remove(this); + } + + if (pool.domain) { + pool.domain.add(this); + } + } + // When a fatal error occurs the connection's protocol ends, which will cause // the connection to end as well, thus we only need to watch for the end event // and we will be notified of disconnects. diff --git a/test/unit/pool/test-connection-domain.js b/test/unit/pool/test-connection-domain.js new file mode 100644 index 000000000..f1ec28e31 --- /dev/null +++ b/test/unit/pool/test-connection-domain.js @@ -0,0 +1,58 @@ +var after = require('after'); +var assert = require('assert'); +var common = require('../../common'); +var domain = null; + +try { + domain = require('domain'); +} catch (e) { + common.skipTest('node ' + process.version + ' does not support domains'); +} + +var d0 = domain.create(); +var d1 = domain.create(); + +var server = common.createFakeServer(); + +var pool = null; +var done = after(2, function () { + pool.end(function (err) { + assert.ifError(err); + server.destroy(); + }); +}); + +server.listen(common.fakeServerPort, function (err) { + assert.ifError(err); + + var released = false; + var timer = setInterval(function () { + if (!released) return; + clearInterval(timer); + + assert.ok(!domain.active, 'no current domain'); + pool.getConnection(function (err, conn) { + assert.ifError(err); + assert.equal(domain.active, d0, 'current domain is d0'); + assert.equal(conn.domain, d0, 'connection domain is d0'); + conn.release(); + done(); + }); + }, 200); + + d0.run(function () { + pool = common.createPool({port: common.fakeServerPort, connectionLimit: 1}); + assert.equal(pool.domain, d0, 'pool belongs to d0'); + + d1.run(function () { + pool.getConnection(function (err, conn) { + assert.ifError(err); + assert.equal(domain.active, d1, 'current domain is d1'); + assert.equal(conn.domain, d0, 'connection domain is d0'); + conn.release(); + released = true; + done(); + }); + }); + }); +}); diff --git a/test/unit/pool/test-connection-no-domain.js b/test/unit/pool/test-connection-no-domain.js new file mode 100644 index 000000000..1c3f8a37f --- /dev/null +++ b/test/unit/pool/test-connection-no-domain.js @@ -0,0 +1,55 @@ +var after = require('after'); +var assert = require('assert'); +var common = require('../../common'); +var domain = null; + +try { + domain = require('domain'); +} catch (e) { + common.skipTest('node ' + process.version + ' does not support domains'); +} + +var d0 = domain.create(); + +var server = common.createFakeServer(); + +var pool = null; +var done = after(2, function () { + pool.end(function (err) { + assert.ifError(err); + server.destroy(); + }); +}); + +server.listen(common.fakeServerPort, function (err) { + assert.ifError(err); + + var released = false; + var timer = setInterval(function () { + if (!released) return; + clearInterval(timer); + + assert.ok(!domain.active, 'no current domain'); + pool.getConnection(function (err, conn) { + assert.ifError(err); + assert.ok(!domain.active, 'no current domain'); + assert.equal(conn.domain, null, 'connection is not bound to domain'); + conn.release(); + done(); + }); + }, 200); + + pool = common.createPool({port: common.fakeServerPort, connectionLimit: 1}); + assert.equal(pool.domain, null, 'pool is not bound to domain'); + + d0.run(function () { + pool.getConnection(function (err, conn) { + assert.ifError(err); + assert.equal(domain.active, d0, 'current domain is d0'); + assert.equal(conn.domain, null, 'connection is not bound to domain'); + conn.release(); + released = true; + done(); + }); + }); +});