Skip to content

Commit f71022d

Browse files
committed
Merge pull request #709 from felixge/domains
Domains support
2 parents ba47cd8 + cb4e356 commit f71022d

File tree

4 files changed

+122
-8
lines changed

4 files changed

+122
-8
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ you spot any mistakes.
77
## HEAD
88

99
* internal parser speed improvement #702
10+
* domains support
1011

1112
## v2.0.0 (2014-01-09)
1213

lib/Connection.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ function Connection(options) {
1919
this.state = "disconnected";
2020
}
2121

22+
function bindToCurrentDomain(cb) {
23+
var domain = process.domain;
24+
if (!domain || !cb)
25+
return cb;
26+
else
27+
return domain.bind(cb);
28+
}
29+
2230
Connection.createQuery = function(sql, values, cb) {
2331
if (sql instanceof Query) {
2432
return sql;
@@ -44,7 +52,7 @@ Connection.createQuery = function(sql, values, cb) {
4452
options.sql = sql;
4553
options.values = values;
4654
}
47-
return new Query(options, cb);
55+
return new Query(options, bindToCurrentDomain(cb));
4856
};
4957

5058
Connection.prototype.connect = function(cb) {
@@ -95,7 +103,7 @@ Connection.prototype.changeUser = function(options, cb){
95103
database : options.database || this.config.database,
96104
charsetNumber : charsetNumber,
97105
currentConfig : this.config
98-
}, cb);
106+
}, bindToCurrentDomain(cb));
99107
};
100108

101109
Connection.prototype.beginTransaction = function(cb) {
@@ -142,17 +150,17 @@ Connection.prototype.query = function(sql, values, cb) {
142150

143151
Connection.prototype.ping = function(cb) {
144152
this._implyConnect();
145-
this._protocol.ping(cb);
153+
this._protocol.ping(bindToCurrentDomain(cb));
146154
};
147155

148156
Connection.prototype.statistics = function(cb) {
149157
this._implyConnect();
150-
this._protocol.stats(cb);
158+
this._protocol.stats(bindToCurrentDomain(cb));
151159
};
152160

153161
Connection.prototype.end = function(cb) {
154162
this._implyConnect();
155-
this._protocol.quit(cb);
163+
this._protocol.quit(bindToCurrentDomain(cb));
156164
};
157165

158166
Connection.prototype.destroy = function() {

lib/Pool.js

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

2121
Pool.prototype.getConnection = function (cb) {
22+
2223
if (this._closed) {
2324
return process.nextTick(function(){
2425
return cb(new Error('Pool is closed.'));
@@ -31,7 +32,7 @@ Pool.prototype.getConnection = function (cb) {
3132
connection = this._freeConnections.shift();
3233

3334
return process.nextTick(function(){
34-
return cb(null, connection);
35+
cb(null, connection);
3536
});
3637
}
3738

@@ -63,6 +64,8 @@ Pool.prototype.getConnection = function (cb) {
6364
return cb(new Error('Queue limit reached.'));
6465
}
6566

67+
if (cb && process.domain)
68+
cb = process.domain.bind(cb);
6669
this._connectionQueue.push(cb);
6770
};
6871

@@ -124,11 +127,11 @@ Pool.prototype.query = function (sql, values, cb) {
124127
cb = values;
125128
values = null;
126129
}
127-
130+
128131
this.getConnection(function (err, conn) {
129132
if (err) return cb(err);
130133

131-
conn.query(sql, values, function () {
134+
conn.query(sql, values, function (err, rows, fields) {
132135
conn.release();
133136
cb.apply(this, arguments);
134137
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var assert = require('assert');
2+
var domain = require('domain');
3+
var d1 = domain.create();
4+
var d2 = domain.create();
5+
var d3 = domain.create();
6+
var d4 = domain.create();
7+
var d5 = domain.create();
8+
var d6 = domain.create();
9+
var d7 = domain.create();
10+
var err1, err2, err3, err4, err5, err6, err7;
11+
12+
d1.run(function() {
13+
var common = require('../../common');
14+
var connection = common.createConnection();
15+
var pool = common.createPool({ connectionLimit: 1});
16+
var assert = require('assert');
17+
18+
d2.run(function() {
19+
connection.query('SELECT 1', function(err, _rows, _fields) {
20+
if (err) throw err;
21+
throw new Error('inside domain 2');
22+
});
23+
});
24+
25+
d3.run(function() {
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();
34+
throw new Error('inside domain 3');
35+
});
36+
});
37+
38+
d4.run(function() {
39+
connection.ping(function() {
40+
throw new Error('inside domain 4');
41+
});
42+
});
43+
44+
d5.run(function() {
45+
connection.statistics(function(err, stat) {
46+
throw new Error('inside domain 5');
47+
});
48+
});
49+
50+
d6.run(function() {
51+
pool.getConnection(function(err, conn) {
52+
conn.release();
53+
throw new Error('inside domain 6');
54+
});
55+
});
56+
57+
connection.end();
58+
setTimeout(function() {
59+
pool.end();
60+
throw new Error('inside domain 1');
61+
}, 100);
62+
63+
d2.on('error', function(err) {
64+
assert.equal(''+err, 'Error: inside domain 2')
65+
err2 = err;
66+
});
67+
d3.on('error', function(err) {
68+
assert.equal(''+err, 'Error: inside domain 3')
69+
err3 = err;
70+
});
71+
d4.on('error', function(err) {
72+
assert.equal(''+err, 'Error: inside domain 4')
73+
err4 = err;
74+
});
75+
d5.on('error', function(err) {
76+
assert.equal(''+err, 'Error: inside domain 5')
77+
err5 = err;
78+
});
79+
d6.on('error', function(err) {
80+
assert.equal(''+err, 'Error: inside domain 6')
81+
err6 = err;
82+
});
83+
d7.on('error', function(err) {
84+
assert.equal(''+err, 'Error: inside domain 7')
85+
err7 = err;
86+
});
87+
});
88+
89+
d1.on('error', function(err) {
90+
assert.equal(''+err, 'Error: inside domain 1');
91+
err1 = err;
92+
});
93+
94+
process.on('exit', function() {
95+
assert.equal(''+err1, 'Error: inside domain 1')
96+
assert.equal(''+err2, 'Error: inside domain 2')
97+
assert.equal(''+err3, 'Error: inside domain 3')
98+
assert.equal(''+err4, 'Error: inside domain 4')
99+
assert.equal(''+err5, 'Error: inside domain 5')
100+
assert.equal(''+err6, 'Error: inside domain 6')
101+
assert.equal(''+err7, 'Error: inside domain 7')
102+
});

0 commit comments

Comments
 (0)