Skip to content

Commit 526a671

Browse files
committed
domains support in pool
1 parent 3f6fc19 commit 526a671

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/Pool.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function Pool(options) {
1919
}
2020

2121
Pool.prototype.getConnection = function (cb) {
22+
23+
if (cb && process.domain)
24+
cb = process.domain.bind(cb);
25+
2226
if (this._closed) {
2327
return process.nextTick(function(){
2428
return cb(new Error('Pool is closed.'));
@@ -31,7 +35,7 @@ Pool.prototype.getConnection = function (cb) {
3135
connection = this._freeConnections.shift();
3236

3337
return process.nextTick(function(){
34-
return cb(null, connection);
38+
cb(null, connection);
3539
});
3640
}
3741

@@ -124,11 +128,11 @@ Pool.prototype.query = function (sql, values, cb) {
124128
cb = values;
125129
values = null;
126130
}
127-
131+
128132
this.getConnection(function (err, conn) {
129133
if (err) return cb(err);
130134

131-
conn.query(sql, values, function () {
135+
conn.query(sql, values, function (err, rows, fields) {
132136
conn.release();
133137
cb.apply(this, arguments);
134138
});

test/integration/connection/test-domains.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ var d3 = domain.create();
66
var d4 = domain.create();
77
var d5 = domain.create();
88
var d6 = domain.create();
9-
var err1, err2, err3, err4, err5, err6;
9+
var d7 = domain.create();
10+
var err1, err2, err3, err4, err5, err6, err7;
1011

1112
d1.run(function() {
1213
var common = require('../../common');
1314
var connection = common.createConnection();
14-
var pool = common.createPool();
15+
var pool = common.createPool({ connectionLimit: 1});
1516
var assert = require('assert');
1617

1718
d2.run(function() {
@@ -20,10 +21,16 @@ d1.run(function() {
2021
throw new Error('inside domain 2');
2122
});
2223
});
23-
24+
2425
d3.run(function() {
25-
pool.query('SELECT 2', function(err, _rows, _fields) {
26-
if (err) throw err;
26+
pool.getConnection(function(err, conn) {
27+
d7.run(function() {
28+
pool.query('SELECT 2', function(err, _rows, _fields) {
29+
if (err) throw err;
30+
throw new Error('inside domain 7');
31+
});
32+
});
33+
conn.release();
2734
throw new Error('inside domain 3');
2835
});
2936
});
@@ -54,23 +61,33 @@ d1.run(function() {
5461
}, 100);
5562

5663
d2.on('error', function(err) {
64+
assert.equal(''+err, 'Error: inside domain 2')
5765
err2 = err;
5866
});
5967
d3.on('error', function(err) {
68+
assert.equal(''+err, 'Error: inside domain 3')
6069
err3 = err;
6170
});
6271
d4.on('error', function(err) {
72+
assert.equal(''+err, 'Error: inside domain 4')
6373
err4 = err;
6474
});
6575
d5.on('error', function(err) {
76+
assert.equal(''+err, 'Error: inside domain 5')
6677
err5 = err;
6778
});
6879
d6.on('error', function(err) {
80+
assert.equal(''+err, 'Error: inside domain 6')
6981
err6 = err;
7082
});
83+
d7.on('error', function(err) {
84+
assert.equal(''+err, 'Error: inside domain 7')
85+
err7 = err;
86+
});
7187
});
7288

7389
d1.on('error', function(err) {
90+
assert.equal(''+err, 'Error: inside domain 1');
7491
err1 = err;
7592
});
7693

@@ -81,4 +98,5 @@ process.on('exit', function() {
8198
assert.equal(''+err4, 'Error: inside domain 4')
8299
assert.equal(''+err5, 'Error: inside domain 5')
83100
assert.equal(''+err6, 'Error: inside domain 6')
101+
assert.equal(''+err7, 'Error: inside domain 7')
84102
});

0 commit comments

Comments
 (0)