Skip to content

Commit d6f6d23

Browse files
committed
Add connection.threadId to get MySQL connection ID
fixes #602
1 parent 7164fd6 commit d6f6d23

File tree

7 files changed

+73
-6
lines changed

7 files changed

+73
-6
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ you spot any mistakes.
1717
* Fix `pool.end` to handle queued connections #797
1818
* Fix `pool.releaseConnection` to keep connection queue flowing #797
1919
* Fix SSL handshake error to be catchable #800
20+
* Add `connection.threadId` to get MySQL connection ID #602
2021

2122
## v2.1.1 (2014-03-13)
2223

Readme.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ var connection = mysql.createConnection({
105105
});
106106

107107
connection.connect(function(err) {
108-
// connected! (unless `err` is set)
108+
if (err) {
109+
console.error('error connecting: ' + err.stack);
110+
return;
111+
}
112+
113+
console.log('connected as id ' + connection.threadId);
109114
});
110115
```
111116

@@ -592,6 +597,18 @@ connection.query('UPDATE posts SET ...', function (err, response) {
592597
})
593598
```
594599

600+
## Getting the connection ID
601+
602+
You can get the MySQL connection ID ("thread ID") of a given connection using the `threadId`
603+
property.
604+
605+
```js
606+
connection.connect(function(err) {
607+
if (err) throw err;
608+
console.log('connected as id ' + connection.threadId);
609+
});
610+
```
611+
595612
## Executing queries in parallel
596613

597614
The MySQL protocol is sequential, this means that you need multiple connections

lib/Connection.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function Connection(options) {
1717
this._protocol = new Protocol({config: this.config, connection: this});
1818
this._connectCalled = false;
1919
this.state = "disconnected";
20+
this.threadId = null;
2021
}
2122

2223
function bindToCurrentDomain(cb) {
@@ -282,8 +283,9 @@ Connection.prototype._handleProtocolConnect = function() {
282283
this.state = "connected";
283284
};
284285

285-
Connection.prototype._handleProtocolHandshake = function() {
286-
this.state = "authenticated";
286+
Connection.prototype._handleProtocolHandshake = function _handleProtocolHandshake(packet) {
287+
this.state = "authenticated";
288+
this.threadId = packet.threadId;
287289
};
288290

289291
Connection.prototype._handleProtocolEnd = function(err) {

lib/protocol/Protocol.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Protocol.prototype._determinePacket = function(sequence) {
229229
case 0x00:
230230
if (!this._handshaked) {
231231
this._handshaked = true;
232-
this.emit('handshake');
232+
this.emit('handshake', this._handshakeInitializationPacket);
233233
}
234234
return Packets.OkPacket;
235235
case 0xfe: return Packets.EofPacket;

test/FakeServer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// An experimental fake MySQL server for tricky integration tests. Expanded
22
// as needed.
33

4+
var _ = require('underscore');
45
var Net = require('net');
56
var Packets = require('../lib/protocol/packets');
67
var PacketWriter = require('../lib/protocol/PacketWriter');
@@ -54,12 +55,14 @@ function FakeConnection(socket) {
5455
FakeConnection.prototype.handshake = function(options) {
5556
this._handshakeOptions = options || {};
5657

57-
this._handshakeInitializationPacket = new Packets.HandshakeInitializationPacket({
58+
var packetOpiotns = _.extend({
5859
scrambleBuff1 : new Buffer('1020304050607080', 'hex'),
5960
scrambleBuff2 : new Buffer('0102030405060708090A0B0C', 'hex'),
6061
serverCapabilities1 : 512, // only 1 flag, PROTOCOL_41
6162
protocol41 : true
62-
});
63+
}, this._handshakeOptions);
64+
65+
this._handshakeInitializationPacket = new Packets.HandshakeInitializationPacket(packetOpiotns);
6366

6467
this._sendPacket(this._handshakeInitializationPacket);
6568
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var connection = common.createConnection();
4+
5+
assert.strictEqual(connection.threadId, null);
6+
7+
connection.connect(function(err) {
8+
assert.ifError(err);
9+
assert.notStrictEqual(connection.threadId, null);
10+
assert.notStrictEqual(connection.threadId, 0);
11+
12+
connection.end(function(err) {
13+
assert.ifError(err);
14+
assert.notStrictEqual(connection.threadId, null);
15+
assert.notStrictEqual(connection.threadId, 0);
16+
});
17+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var connection = common.createConnection({port: common.fakeServerPort});
4+
5+
var server = common.createFakeServer();
6+
7+
server.listen(common.fakeServerPort, function(err) {
8+
assert.ifError(err);
9+
assert.strictEqual(connection.threadId, null);
10+
11+
connection.connect(function(err) {
12+
assert.ifError(err);
13+
assert.strictEqual(connection.threadId, 42);
14+
15+
connection.end(function(err) {
16+
assert.ifError(err);
17+
assert.strictEqual(connection.threadId, 42);
18+
server.destroy();
19+
});
20+
});
21+
});
22+
23+
server.on('connection', function(incomingConnection) {
24+
incomingConnection.handshake({
25+
threadId : 42
26+
});
27+
});

0 commit comments

Comments
 (0)