@@ -74,11 +74,12 @@ Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
74
74
throw new Error ( 'Connection acquired from wrong pool.' ) ;
75
75
}
76
76
77
- var pool = this ;
77
+ var changeUser = this . _needsChangeUser ( connection ) ;
78
+ var pool = this ;
78
79
79
80
this . _acquiringConnections . push ( connection ) ;
80
81
81
- connection . ping ( { timeout : this . config . acquireTimeout } , function onPing ( err ) {
82
+ function onOperationComplete ( err ) {
82
83
spliceConnection ( pool . _acquiringConnections , connection ) ;
83
84
84
85
if ( pool . _closed ) {
@@ -91,12 +92,26 @@ Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
91
92
return ;
92
93
}
93
94
95
+ if ( changeUser ) {
96
+ pool . emit ( 'connection' , connection ) ;
97
+ }
98
+
94
99
cb ( null , connection ) ;
95
- } ) ;
100
+ }
101
+
102
+ if ( changeUser ) {
103
+ // restore user back to pool configuration
104
+ connection . config = this . config . newConnectionConfig ( ) ;
105
+ connection . changeUser ( { timeout : this . config . acquireTimeout } , onOperationComplete ) ;
106
+ } else {
107
+ // ping connection
108
+ connection . ping ( { timeout : this . config . acquireTimeout } , onOperationComplete ) ;
109
+ }
96
110
} ;
97
111
98
112
Pool . prototype . releaseConnection = function releaseConnection ( connection ) {
99
113
var cb ;
114
+ var pool = this ;
100
115
101
116
if ( this . _acquiringConnections . indexOf ( connection ) !== - 1 ) {
102
117
// connection is being acquired
@@ -108,11 +123,7 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) {
108
123
throw new Error ( 'Connection released to wrong pool' ) ;
109
124
}
110
125
111
- if ( connection . _purge ) {
112
- // purge connection from pool
113
- this . _purgeConnection ( connection ) ;
114
- return ;
115
- } else if ( this . _freeConnections . indexOf ( connection ) !== - 1 ) {
126
+ if ( this . _freeConnections . indexOf ( connection ) !== - 1 ) {
116
127
// connection already in free connection pool
117
128
// this won't catch all double-release cases
118
129
throw new Error ( 'Connection already released' ) ;
@@ -218,6 +229,17 @@ Pool.prototype._enqueueCallback = function _enqueueCallback(callback) {
218
229
this . emit ( 'enqueue' ) ;
219
230
} ;
220
231
232
+ Pool . prototype . _needsChangeUser = function _needsChangeUser ( connection ) {
233
+ var connConfig = connection . config ;
234
+ var poolConfig = this . config . connectionConfig ;
235
+
236
+ // check if changeUser values are different
237
+ return connConfig . user !== poolConfig . user
238
+ || connConfig . database !== poolConfig . database
239
+ || connConfig . password !== poolConfig . password
240
+ || connConfig . charsetNumber !== poolConfig . charsetNumber ;
241
+ }
242
+
221
243
Pool . prototype . _purgeConnection = function _purgeConnection ( connection , callback ) {
222
244
var cb = callback || function ( ) { } ;
223
245
0 commit comments