1
-
2
- var Mysql = require ( '../' ) ;
1
+ var mysql = require ( '../' ) ;
3
2
var Connection = require ( './Connection' ) ;
4
3
5
4
module . exports = Pool ;
5
+
6
6
function Pool ( options ) {
7
7
this . config = options . config ;
8
8
this . config . connectionConfig . pool = this ;
@@ -13,81 +13,94 @@ function Pool(options) {
13
13
this . _closed = false ;
14
14
}
15
15
16
- Pool . prototype . getConnection = function ( cb ) {
16
+ Pool . prototype . getConnection = function ( cb ) {
17
17
if ( this . _closed ) {
18
- cb ( new Error ( 'Pool is closed.' ) ) ;
19
- return ;
18
+ return cb ( new Error ( 'Pool is closed.' ) ) ;
20
19
}
21
20
21
+ var connection ;
22
+
22
23
if ( this . _freeConnections . length > 0 ) {
23
- var connection = this . _freeConnections [ 0 ] ;
24
- this . _freeConnections . shift ( ) ;
25
- cb ( null , connection ) ;
26
- } else if ( this . config . connectionLimit == 0 || this . _allConnections . length < this . config . connectionLimit ) {
27
- var self = this ;
28
- var connection = this . _createConnection ( ) ;
24
+ connection = this . _freeConnections . shift ( ) ;
25
+
26
+ return cb ( null , connection ) ;
27
+ }
28
+
29
+ if ( this . config . connectionLimit === 0 || this . _allConnections . length < this . config . connectionLimit ) {
30
+ connection = this . _createConnection ( ) ;
31
+
29
32
this . _allConnections . push ( connection ) ;
30
- connection . connect ( function ( err ) {
31
- if ( self . _closed ) {
32
- cb ( new Error ( 'Pool is closed.' ) ) ;
33
+
34
+ return connection . connect ( function ( err ) {
35
+ if ( this . _closed ) {
36
+ return cb ( new Error ( 'Pool is closed.' ) ) ;
33
37
}
34
- else if ( err ) {
35
- cb ( err ) ;
36
- } else {
37
- cb ( null , connection ) ;
38
+ if ( err ) {
39
+ return cb ( err ) ;
38
40
}
39
- } ) ;
40
- } else if ( this . config . waitForConnections ) {
41
- this . _connectionQueue . push ( cb ) ;
42
- } else {
43
- cb ( new Error ( 'No connections available.' ) ) ;
41
+
42
+ return cb ( null , connection ) ;
43
+ } . bind ( this ) ) ;
44
+ }
45
+
46
+ if ( ! this . config . waitForConnections ) {
47
+ return cb ( new Error ( 'No connections available.' ) ) ;
44
48
}
49
+
50
+ this . _connectionQueue . push ( cb ) ;
45
51
} ;
46
52
47
- Pool . prototype . releaseConnection = function ( connection ) {
53
+ Pool . prototype . releaseConnection = function ( connection ) {
54
+ var cb ;
55
+
48
56
if ( connection . _poolRemoved ) {
49
57
// The connection has been removed from the pool and is no longer good.
50
58
if ( this . _connectionQueue . length ) {
51
- var cb = this . _connectionQueue [ 0 ] ;
52
- this . _connectionQueue . shift ( ) ;
59
+ cb = this . _connectionQueue . shift ( ) ;
60
+
53
61
process . nextTick ( this . getConnection . bind ( this , cb ) ) ;
54
62
}
55
63
} else if ( this . _connectionQueue . length ) {
56
- var cb = this . _connectionQueue [ 0 ] ;
57
- this . _connectionQueue . shift ( ) ;
64
+ cb = this . _connectionQueue . shift ( ) ;
65
+
58
66
process . nextTick ( cb . bind ( null , null , connection ) ) ;
59
67
} else {
60
68
this . _freeConnections . push ( connection ) ;
61
69
}
62
70
} ;
63
71
64
- Pool . prototype . end = function ( cb ) {
72
+ Pool . prototype . end = function ( cb ) {
65
73
this . _closed = true ;
66
- cb = cb || function ( err ) { if ( err ) throw err ; } ;
67
- var self = this ;
68
- var closedConnections = 0 ;
74
+
75
+ if ( typeof cb != "function" ) {
76
+ cb = function ( err ) {
77
+ if ( err ) throw err ;
78
+ } ;
79
+ }
80
+
69
81
var calledBack = false ;
82
+ var closedConnections = 0 ;
83
+ var connection ;
84
+
70
85
var endCB = function ( err ) {
71
86
if ( calledBack ) {
72
87
return ;
73
- } else if ( err ) {
74
- calledBack = true ;
75
- delete endCB ;
76
- cb ( err ) ;
77
- } else if ( ++ closedConnections >= self . _allConnections . length ) {
88
+ }
89
+
90
+ if ( err || ++ closedConnections >= this . _allConnections . length ) {
78
91
calledBack = true ;
79
92
delete endCB ;
80
- cb ( ) ;
93
+ return cb ( err ) ;
81
94
}
82
- } ;
95
+ } . bind ( this ) ;
83
96
84
- if ( this . _allConnections . length == 0 ) {
85
- endCB ( ) ;
86
- return ;
97
+ if ( this . _allConnections . length === 0 ) {
98
+ return endCB ( ) ;
87
99
}
88
100
89
- for ( var i = 0 ; i < this . _allConnections . length ; ++ i ) {
90
- var connection = this . _allConnections [ i ] ;
101
+ for ( var i = 0 ; i < this . _allConnections . length ; i ++ ) {
102
+ connection = this . _allConnections [ i ] ;
103
+
91
104
connection . destroy = connection . _realDestroy ;
92
105
connection . end = connection . _realEnd ;
93
106
connection . end ( endCB ) ;
@@ -98,7 +111,7 @@ Pool.prototype._createConnection = function() {
98
111
var self = this ;
99
112
var connection = ( this . config . createConnection )
100
113
? this . config . createConnection ( this . config . connectionConfig )
101
- : Mysql . createConnection ( this . config . connectionConfig ) ;
114
+ : mysql . createConnection ( this . config . connectionConfig ) ;
102
115
103
116
connection . _realEnd = connection . end ;
104
117
connection . end = function ( cb ) {
@@ -136,14 +149,18 @@ Pool.prototype._handleConnectionError = function(connection) {
136
149
} ;
137
150
138
151
Pool . prototype . _removeConnection = function ( connection ) {
152
+ var i ;
153
+
139
154
connection . _poolRemoved = true ;
140
- for ( var i = 0 ; i < this . _allConnections . length ; ++ i ) {
155
+
156
+ for ( i = 0 ; i < this . _allConnections . length ; i ++ ) {
141
157
if ( this . _allConnections [ i ] === connection ) {
142
158
this . _allConnections . splice ( i , 1 ) ;
143
159
break ;
144
160
}
145
161
}
146
- for ( var i = 0 ; i < this . _freeConnections . length ; ++ i ) {
162
+
163
+ for ( i = 0 ; i < this . _freeConnections . length ; i ++ ) {
147
164
if ( this . _freeConnections [ i ] === connection ) {
148
165
this . _freeConnections . splice ( i , 1 ) ;
149
166
break ;
@@ -152,5 +169,6 @@ Pool.prototype._removeConnection = function(connection) {
152
169
153
170
connection . end = connection . _realEnd ;
154
171
connection . destroy = connection . _realDestroy ;
172
+
155
173
this . releaseConnection ( connection ) ;
156
174
} ;
0 commit comments