@@ -22,28 +22,16 @@ function Connection(options) {
22
22
this . threadId = null ;
23
23
}
24
24
25
- function bindToCurrentDomain ( callback ) {
26
- if ( ! callback ) {
27
- return undefined ;
28
- }
29
-
30
- var domain = process . domain ;
31
-
32
- return domain
33
- ? domain . bind ( callback )
34
- : callback ;
35
- }
36
-
37
25
Connection . createQuery = function createQuery ( sql , values , callback ) {
38
26
if ( sql instanceof Query ) {
39
27
return sql ;
40
28
}
41
29
42
- var cb = bindToCurrentDomain ( callback ) ;
30
+ var cb = wrapCallbackInDomain ( null , callback ) ;
43
31
var options = { } ;
44
32
45
33
if ( typeof sql === 'function' ) {
46
- cb = bindToCurrentDomain ( sql ) ;
34
+ cb = wrapCallbackInDomain ( null , sql ) ;
47
35
return new Query ( options , cb ) ;
48
36
}
49
37
@@ -53,7 +41,7 @@ Connection.createQuery = function createQuery(sql, values, callback) {
53
41
}
54
42
55
43
if ( typeof values === 'function' ) {
56
- cb = bindToCurrentDomain ( values ) ;
44
+ cb = wrapCallbackInDomain ( null , values ) ;
57
45
} else if ( values !== undefined ) {
58
46
options . values = values ;
59
47
}
@@ -65,7 +53,7 @@ Connection.createQuery = function createQuery(sql, values, callback) {
65
53
options . values = values ;
66
54
67
55
if ( typeof values === 'function' ) {
68
- cb = bindToCurrentDomain ( values ) ;
56
+ cb = wrapCallbackInDomain ( null , values ) ;
69
57
options . values = undefined ;
70
58
}
71
59
@@ -99,15 +87,15 @@ Connection.prototype.connect = function connect(options, callback) {
99
87
this . _protocol . on ( 'data' , function ( data ) {
100
88
connection . _socket . write ( data ) ;
101
89
} ) ;
102
- this . _socket . on ( 'data' , function ( data ) {
90
+ this . _socket . on ( 'data' , wrapToDomain ( connection , function ( data ) {
103
91
connection . _protocol . write ( data ) ;
104
- } ) ;
92
+ } ) ) ;
105
93
this . _protocol . on ( 'end' , function ( ) {
106
94
connection . _socket . end ( ) ;
107
95
} ) ;
108
- this . _socket . on ( 'end' , function ( ) {
96
+ this . _socket . on ( 'end' , wrapToDomain ( connection , function ( ) {
109
97
connection . _protocol . end ( ) ;
110
- } ) ;
98
+ } ) ) ;
111
99
112
100
this . _socket . on ( 'error' , this . _handleNetworkError . bind ( this ) ) ;
113
101
this . _socket . on ( 'connect' , this . _handleProtocolConnect . bind ( this ) ) ;
@@ -127,7 +115,7 @@ Connection.prototype.connect = function connect(options, callback) {
127
115
}
128
116
}
129
117
130
- this . _protocol . handshake ( options , bindToCurrentDomain ( callback ) ) ;
118
+ this . _protocol . handshake ( options , wrapCallbackInDomain ( this , callback ) ) ;
131
119
} ;
132
120
133
121
Connection . prototype . changeUser = function changeUser ( options , callback ) {
@@ -149,7 +137,7 @@ Connection.prototype.changeUser = function changeUser(options, callback) {
149
137
timeout : options . timeout ,
150
138
charsetNumber : charsetNumber ,
151
139
currentConfig : this . config
152
- } , bindToCurrentDomain ( callback ) ) ;
140
+ } , wrapCallbackInDomain ( this , callback ) ) ;
153
141
} ;
154
142
155
143
Connection . prototype . beginTransaction = function beginTransaction ( options , callback ) {
@@ -203,6 +191,10 @@ Connection.prototype.query = function query(sql, values, cb) {
203
191
query . sql = this . format ( query . sql , query . values ) ;
204
192
}
205
193
194
+ if ( query . _callback ) {
195
+ query . _callback = wrapCallbackInDomain ( this , query . _callback ) ;
196
+ }
197
+
206
198
this . _implyConnect ( ) ;
207
199
208
200
return this . _protocol . _enqueue ( query ) ;
@@ -215,7 +207,7 @@ Connection.prototype.ping = function ping(options, callback) {
215
207
}
216
208
217
209
this . _implyConnect ( ) ;
218
- this . _protocol . ping ( options , bindToCurrentDomain ( callback ) ) ;
210
+ this . _protocol . ping ( options , wrapCallbackInDomain ( this , callback ) ) ;
219
211
} ;
220
212
221
213
Connection . prototype . statistics = function statistics ( options , callback ) {
@@ -225,7 +217,7 @@ Connection.prototype.statistics = function statistics(options, callback) {
225
217
}
226
218
227
219
this . _implyConnect ( ) ;
228
- this . _protocol . stats ( options , bindToCurrentDomain ( callback ) ) ;
220
+ this . _protocol . stats ( options , wrapCallbackInDomain ( this , callback ) ) ;
229
221
} ;
230
222
231
223
Connection . prototype . end = function end ( options , callback ) {
@@ -246,7 +238,7 @@ Connection.prototype.end = function end(options, callback) {
246
238
}
247
239
248
240
this . _implyConnect ( ) ;
249
- this . _protocol . quit ( opts , bindToCurrentDomain ( cb ) ) ;
241
+ this . _protocol . quit ( opts , wrapCallbackInDomain ( this , cb ) ) ;
250
242
} ;
251
243
252
244
Connection . prototype . destroy = function ( ) {
@@ -461,3 +453,53 @@ Connection.prototype._implyConnect = function() {
461
453
this . connect ( ) ;
462
454
}
463
455
} ;
456
+
457
+ function unwrapFromDomain ( fn ) {
458
+ return function ( ) {
459
+ var domains = [ ] ;
460
+ var ret ;
461
+
462
+ while ( process . domain ) {
463
+ domains . shift ( process . domain ) ;
464
+ process . domain . exit ( ) ;
465
+ }
466
+
467
+ try {
468
+ ret = fn . apply ( this , arguments ) ;
469
+ } finally {
470
+ for ( var i = 0 ; i < domains . length ; i ++ ) {
471
+ domains [ i ] . enter ( ) ;
472
+ }
473
+ }
474
+
475
+ return ret ;
476
+ } ;
477
+ }
478
+
479
+ function wrapCallbackInDomain ( ee , fn ) {
480
+ if ( typeof fn !== 'function' || fn . domain ) {
481
+ return fn ;
482
+ }
483
+
484
+ var domain = process . domain ;
485
+
486
+ if ( domain ) {
487
+ return domain . bind ( fn ) ;
488
+ } else if ( ee ) {
489
+ return unwrapFromDomain ( wrapToDomain ( ee , fn ) ) ;
490
+ } else {
491
+ return fn ;
492
+ }
493
+ }
494
+
495
+ function wrapToDomain ( ee , fn ) {
496
+ return function ( ) {
497
+ if ( Events . usingDomains && ee . domain ) {
498
+ ee . domain . enter ( ) ;
499
+ fn . apply ( this , arguments ) ;
500
+ ee . domain . exit ( ) ;
501
+ } else {
502
+ fn . apply ( this , arguments ) ;
503
+ }
504
+ } ;
505
+ }
0 commit comments