diff --git a/lib/Pool.js b/lib/Pool.js index d1eb71a80..778b7abf1 100644 --- a/lib/Pool.js +++ b/lib/Pool.js @@ -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; @@ -34,11 +38,16 @@ Pool.prototype.getConnection = function(cb) { else if (err) { cb(err); } else { + self.emit('connection', null, connection); cb(null, connection); } }); } else if (this.config.waitForConnections) { - this._connectionQueue.push(cb); + if (this.config.queueLimit && this._connectionQueue.length >= this.config.queueLimit) { + cb(new Error('Queue limit reached.')) + } else { + this._connectionQueue.push(cb); + } } else { cb(new Error('No connections available.')); } diff --git a/lib/PoolConfig.js b/lib/PoolConfig.js index 838382fa7..455c8ee17 100644 --- a/lib/PoolConfig.js +++ b/lib/PoolConfig.js @@ -11,4 +11,7 @@ function PoolConfig(options) { this.connectionLimit = (options.connectionLimit === undefined) ? 10 : Number(options.connectionLimit); + this.queueLimit = (options.queueLimit === undefined) + ? 0 + : Number(options.queueLimit); } diff --git a/test/common.js b/test/common.js index 4984228da..808499a2a 100644 --- a/test/common.js +++ b/test/common.js @@ -31,7 +31,9 @@ common.createConnection = function(config) { common.createPool = function(config) { config = mergeTestConfig(config); - config.createConnection = common.createConnection; + if (!config.createConnection) { + config.createConnection = common.createConnection; + } return Mysql.createPool(config); }; diff --git a/test/integration/pool/test-create-connection.js b/test/integration/pool/test-create-connection.js new file mode 100644 index 000000000..4d895393a --- /dev/null +++ b/test/integration/pool/test-create-connection.js @@ -0,0 +1,15 @@ +var common = require('../../common'); +var assert = require('assert'); +var Connection = require(common.lib + '/Connection'); +var pool = common.createPool({ + createConnection: function() { + var connection = common.createConnection() + connection.query('SET SESSION sql_mode="STRICT_ALL_TABLES"') + return connection + } +}); + +pool.getConnection(function(err, connection) { + if (err) throw err; + pool.end() +}); diff --git a/test/integration/pool/test-queue-limit.js b/test/integration/pool/test-queue-limit.js new file mode 100644 index 000000000..9938be288 --- /dev/null +++ b/test/integration/pool/test-queue-limit.js @@ -0,0 +1,28 @@ +var common = require('../../common'); +var assert = require('assert'); +var pool = common.createPool({ + connectionLimit : 1, + queueLimit : 1, + waitForConnections : true +}); + +// First connection we get right away +pool.getConnection(function(err, connection) { + connection.end() +}) + +// Second connection request goes into the queue +pool.getConnection(function(err, connection) { + connection.end() + pool.end() +}) + +// Third connection request gets refused, since the queue is full +var thirdGetErr +pool.getConnection(function(err, connection) { + thirdGetErr = err +}) + +process.on('exit', function() { + assert.equal(thirdGetErr.message, 'Queue limit reached.') +}) diff --git a/test/integration/pool/test-set-session-vars.js b/test/integration/pool/test-set-session-vars.js new file mode 100644 index 000000000..f11e284ee --- /dev/null +++ b/test/integration/pool/test-set-session-vars.js @@ -0,0 +1,16 @@ +var common = require('../../common'); +var assert = require('assert'); +var Connection = require(common.lib + '/Connection'); +var pool = common.createPool(); + +var wasSet = false; +pool.on('connection', function(err, connection) { + connection.query('SET SESSION sql_mode="STRICT_ALL_TABLES"'); + wasSet = true; +}) + +pool.getConnection(function(err, connection) { + if (err) throw err; + assert.equal(wasSet, true); + pool.end(); +});