Skip to content

Add Pool connection event, which fires when the Pool has created a new connection #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ pool.getConnection(function(err, connection) {
});
```

If you need to set session variables on the connection before it gets used,
you can listen to the `connection` event.

```js
pool.on('connection', function(err, connection) {
connection.query('SET SESSION auto_increment_increment=1')
})
```

When you are done with a connection, just call `connection.end()` and the
connection will return to the pool, ready to be used again by someone else.

Expand Down
5 changes: 5 additions & 0 deletions lib/Pool.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
var mysql = require('../');
var Connection = require('./Connection');
var EventEmitter = require('events').EventEmitter;
var Util = require('util');

module.exports = Pool;

Util.inherits(Pool, EventEmitter);
function Pool(options) {
EventEmitter.call(this);
this.config = options.config;
this.config.connectionConfig.pool = this;

Expand Down Expand Up @@ -39,6 +43,7 @@ Pool.prototype.getConnection = function (cb) {
return cb(err);
}

this.emit('connection', null, connection);
return cb(null, connection);
}.bind(this));
}
Expand Down
15 changes: 15 additions & 0 deletions test/integration/pool/test-connection-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var common = require('../../common');
var assert = require('assert');
var Connection = require(common.lib + '/Connection');
var pool = common.createPool();

var connectionEventHappened = false;
pool.on('connection', function(err, connection) {
connectionEventHappened = true;
})

pool.getConnection(function(err, connection) {
if (err) throw err;
assert.equal(connectionEventHappened, true);
pool.end();
});