Skip to content

Commit ea9a27c

Browse files
committed
Clone connection config for new pool connections
fixes #805
1 parent 5003ce7 commit ea9a27c

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

Changes.md

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

77
## HEAD
88

9+
* Clone connection config for new pool connections
910
* Default `connectTimeout` to 2 minutes
1011

1112
## v2.2.0 (2014-04-27)

lib/Pool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Pool.prototype.getConnection = function (cb) {
3535
}
3636

3737
if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) {
38-
connection = new PoolConnection(this, { config: this.config.connectionConfig });
38+
connection = new PoolConnection(this, { config: this.config.newConnectionConfig() });
3939

4040
this._allConnections.push(connection);
4141

lib/PoolConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ function PoolConfig(options) {
1414
? 0
1515
: Number(options.queueLimit);
1616
}
17+
18+
PoolConfig.prototype.newConnectionConfig = function newConnectionConfig() {
19+
var connectionConfig = new ConnectionConfig(this.connectionConfig);
20+
21+
connectionConfig.clientFlags = this.connectionConfig.clientFlags;
22+
connectionConfig.maxPacketSize = this.connectionConfig.maxPacketSize;
23+
24+
return connectionConfig;
25+
};

lib/protocol/packets/ComChangeUserPacket.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function ComChangeUserPacket(options) {
1010
}
1111

1212
ComChangeUserPacket.prototype.parse = function(parser) {
13+
this.command = parser.parseUnsignedNumber(1);
1314
this.user = parser.parseNullTerminatedString();
1415
this.scrambleBuff = parser.parseLengthCodedBuffer();
1516
this.database = parser.parseNullTerminatedString();

test/FakeServer.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ FakeConnection.prototype._parsePacket = function(header) {
135135
this._sendPacket(new Packets.OkPacket());
136136
this._parser.resetPacketNumber();
137137
break;
138+
case Packets.ComChangeUserPacket:
139+
this._clientAuthenticationPacket = new Packets.ClientAuthenticationPacket({
140+
clientFlags : this._clientAuthenticationPacket.clientFlags,
141+
filler : this._clientAuthenticationPacket.filler,
142+
maxPacketSize: this._clientAuthenticationPacket.maxPacketSize,
143+
protocol41 : this._clientAuthenticationPacket.protocol41,
144+
charsetNumber: packet.charsetNumber,
145+
database : packet.database,
146+
scrambleBuff : packet.scrambleBuff,
147+
user : packet.user
148+
});
149+
this._sendPacket(new Packets.OkPacket());
150+
this._parser.resetPacketNumber();
151+
break;
138152
case Packets.ComQuitPacket:
139153
this.emit('quit', packet);
140154
this._socket.end();
@@ -156,6 +170,7 @@ FakeConnection.prototype._determinePacket = function() {
156170
case 0x01: return Packets.ComQuitPacket;
157171
case 0x03: return Packets.ComQueryPacket;
158172
case 0x0e: return Packets.ComPingPacket;
173+
case 0x11: return Packets.ComChangeUserPacket;
159174
default:
160175
throw new Error('Unknown packet, first byte: ' + firstByte);
161176
break;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var pool = common.createPool({
4+
user: 'user_1',
5+
port: common.fakeServerPort
6+
});
7+
8+
var server = common.createFakeServer();
9+
10+
server.listen(common.fakeServerPort, function(err) {
11+
assert.ifError(err);
12+
13+
assert.strictEqual(pool.config.connectionConfig.user, 'user_1');
14+
15+
pool.getConnection(function(err, conn) {
16+
assert.ifError(err);
17+
assert.strictEqual(conn.config.user, 'user_1');
18+
19+
conn.changeUser({user: 'user_2'}, function(err) {
20+
assert.ifError(err);
21+
assert.strictEqual(conn.config.user, 'user_2');
22+
assert.strictEqual(pool.config.connectionConfig.user, 'user_1');
23+
24+
conn.destroy();
25+
server.destroy();
26+
});
27+
});
28+
});
29+
30+
server.on('connection', function(incomingConnection) {
31+
incomingConnection.handshake();
32+
});

0 commit comments

Comments
 (0)