Skip to content

Commit d536023

Browse files
committed
Merge pull request #486 from felixge/pool-connection-event
Add Pool `connection` event, which fires when the Pool has created a new connection
2 parents e8ad4af + fe4a745 commit d536023

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Readme.md

+9
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ pool.getConnection(function(err, connection) {
214214
});
215215
```
216216

217+
If you need to set session variables on the connection before it gets used,
218+
you can listen to the `connection` event.
219+
220+
```js
221+
pool.on('connection', function(err, connection) {
222+
connection.query('SET SESSION auto_increment_increment=1')
223+
})
224+
```
225+
217226
When you are done with a connection, just call `connection.end()` and the
218227
connection will return to the pool, ready to be used again by someone else.
219228

lib/Pool.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
var mysql = require('../');
22
var Connection = require('./Connection');
3+
var EventEmitter = require('events').EventEmitter;
4+
var Util = require('util');
35

46
module.exports = Pool;
57

8+
Util.inherits(Pool, EventEmitter);
69
function Pool(options) {
10+
EventEmitter.call(this);
711
this.config = options.config;
812
this.config.connectionConfig.pool = this;
913

@@ -39,6 +43,7 @@ Pool.prototype.getConnection = function (cb) {
3943
return cb(err);
4044
}
4145

46+
this.emit('connection', null, connection);
4247
return cb(null, connection);
4348
}.bind(this));
4449
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var common = require('../../common');
2+
var assert = require('assert');
3+
var Connection = require(common.lib + '/Connection');
4+
var pool = common.createPool();
5+
6+
var connectionEventHappened = false;
7+
pool.on('connection', function(err, connection) {
8+
connectionEventHappened = true;
9+
})
10+
11+
pool.getConnection(function(err, connection) {
12+
if (err) throw err;
13+
assert.equal(connectionEventHappened, true);
14+
pool.end();
15+
});

0 commit comments

Comments
 (0)