Skip to content

Commit 874f51c

Browse files
committed
Add Pool#connection event, which emits when an open connection has succesfully been added to the pool, but before any Pool#getConnection callbacks are called. This allows for a pool of connections to e.g. set MySQL session variables on each of its connections before they are used
1 parent f8ded79 commit 874f51c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/Pool.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11

22
var Mysql = require('../');
33
var Connection = require('./Connection');
4+
var EventEmitter = require('events').EventEmitter;
5+
var Util = require('util');
46

57
module.exports = Pool;
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

@@ -34,6 +38,7 @@ Pool.prototype.getConnection = function(cb) {
3438
else if (err) {
3539
cb(err);
3640
} else {
41+
self.emit('connection', null, connection);
3742
cb(null, connection);
3843
}
3944
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 wasSet = false;
7+
pool.on('connection', function(err, connection) {
8+
connection.query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
9+
wasSet = true;
10+
})
11+
12+
pool.getConnection(function(err, connection) {
13+
if (err) throw err;
14+
assert.equal(wasSet, true);
15+
pool.end();
16+
});

0 commit comments

Comments
 (0)