@@ -2,6 +2,7 @@ var mysql = require('../');
2
2
var Connection = require ( './Connection' ) ;
3
3
var EventEmitter = require ( 'events' ) . EventEmitter ;
4
4
var Util = require ( 'util' ) ;
5
+ var PoolConnection = require ( './PoolConnection' ) ;
5
6
6
7
module . exports = Pool ;
7
8
@@ -35,7 +36,7 @@ Pool.prototype.getConnection = function (cb) {
35
36
}
36
37
37
38
if ( this . config . connectionLimit === 0 || this . _allConnections . length < this . config . connectionLimit ) {
38
- connection = this . _createConnection ( ) ;
39
+ connection = new PoolConnection ( this , { config : this . config . connectionConfig } ) ;
39
40
40
41
this . _allConnections . push ( connection ) ;
41
42
@@ -68,7 +69,7 @@ Pool.prototype.getConnection = function (cb) {
68
69
Pool . prototype . releaseConnection = function ( connection ) {
69
70
var cb ;
70
71
71
- if ( connection . _poolRemoved ) {
72
+ if ( ! connection . _pool ) {
72
73
// The connection has been removed from the pool and is no longer good.
73
74
if ( this . _connectionQueue . length ) {
74
75
cb = this . _connectionQueue . shift ( ) ;
@@ -115,11 +116,7 @@ Pool.prototype.end = function (cb) {
115
116
116
117
for ( var i = 0 ; i < this . _allConnections . length ; i ++ ) {
117
118
connection = this . _allConnections [ i ] ;
118
-
119
- delete connection . release ;
120
- connection . destroy = connection . _realDestroy ;
121
- connection . end = connection . _realEnd ;
122
- connection . end ( endCB ) ;
119
+ connection . _realEnd ( endCB ) ;
123
120
}
124
121
} ;
125
122
@@ -139,61 +136,9 @@ Pool.prototype.query = function (sql, values, cb) {
139
136
} ) ;
140
137
} ;
141
138
142
- Pool . prototype . _createConnection = function ( ) {
143
- var self = this ;
144
- var connection = ( this . config . createConnection )
145
- ? this . config . createConnection ( this . config . connectionConfig )
146
- : mysql . createConnection ( this . config . connectionConfig ) ;
147
-
148
- connection . _realEnd = connection . end ;
149
- connection . end = function ( cb ) {
150
- console . warn ( 'Calling conn.end() to release a pooled connection is '
151
- + 'deprecated. In next version calling conn.end() will be '
152
- + 'restored to default conn.end() behavior. Use '
153
- + 'conn.release() instead.'
154
- ) ;
155
- this . release ( ) ;
156
- if ( cb ) cb ( ) ;
157
- } ;
158
-
159
- connection . release = function ( ) {
160
- self . releaseConnection ( connection ) ;
161
- } ;
162
-
163
- connection . _realDestroy = connection . destroy ;
164
- connection . destroy = function ( ) {
165
- self . _removeConnection ( connection ) ;
166
- connection . destroy ( ) ;
167
- } ;
168
-
169
- // When a fatal error occurs the connection's protocol ends, which will cause
170
- // the connection to end as well, thus we only need to watch for the end event
171
- // and we will be notified of disconnects.
172
- connection . on ( 'end' , this . _handleConnectionEnd . bind ( this , connection ) ) ;
173
- connection . on ( 'error' , this . _handleConnectionError . bind ( this , connection ) ) ;
174
-
175
- return connection ;
176
- } ;
177
-
178
- Pool . prototype . _handleConnectionEnd = function ( connection ) {
179
- if ( this . _closed || connection . _poolRemoved ) {
180
- return ;
181
- }
182
- this . _removeConnection ( connection ) ;
183
- } ;
184
-
185
- Pool . prototype . _handleConnectionError = function ( connection ) {
186
- if ( this . _closed || connection . _poolRemoved ) {
187
- return ;
188
- }
189
- this . _removeConnection ( connection ) ;
190
- } ;
191
-
192
139
Pool . prototype . _removeConnection = function ( connection ) {
193
140
var i ;
194
141
195
- connection . _poolRemoved = true ;
196
-
197
142
for ( i = 0 ; i < this . _allConnections . length ; i ++ ) {
198
143
if ( this . _allConnections [ i ] === connection ) {
199
144
this . _allConnections . splice ( i , 1 ) ;
@@ -208,9 +153,5 @@ Pool.prototype._removeConnection = function(connection) {
208
153
}
209
154
}
210
155
211
- delete connection . release ;
212
- connection . end = connection . _realEnd ;
213
- connection . destroy = connection . _realDestroy ;
214
-
215
156
this . releaseConnection ( connection ) ;
216
157
} ;
0 commit comments