Skip to content

Commit

Permalink
Fix MSSQL disconnect handling (sequelize#5967)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik authored and janmeier committed May 28, 2016
1 parent d254564 commit c94219c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Future (4.0.0-pre1)
- [FIXED] Pass ResourceLock instead of raw connection in MSSQL disconnect handling
- [CHANGED] Remove `hookValidate` in favor of `validate` with `hooks: true | false`.
- [REMOVED] Support for `referencesKey`
- [CHANGED] Throw if `dialect` is not provided to the constructor
Expand Down
7 changes: 4 additions & 3 deletions lib/dialects/mssql/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ ConnectionManager.prototype.connect = function(config) {
});
}

var connection = new self.lib.Connection(connectionConfig);
var connection = new self.lib.Connection(connectionConfig)
, connectionLock = new ResourceLock(connection);
connection.lib = self.lib;

connection.on('connect', function(err) {
if (!err) {
resolve(new ResourceLock(connection));
resolve(connectionLock);
return;
}

Expand Down Expand Up @@ -115,7 +116,7 @@ ConnectionManager.prototype.connect = function(config) {
switch (err.code) {
case 'ESOCKET':
case 'ECONNRESET':
self.pool.destroy(connection);
self.pool.destroy(connectionLock);
}
});
}
Expand Down
42 changes: 42 additions & 0 deletions test/integration/dialects/mssql/connection-manager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require('../../support')
, dialect = Support.getTestDialect();

if (dialect.match(/^mssql/)) {
describe('[MSSQL Specific] Query Queue', function () {
it('should work with handleDisconnects', function() {
var sequelize = Support.createSequelizeInstance({pool: {min: 1, max: 1, idle: 5000}})
, cm = sequelize.connectionManager
, conn;

return sequelize.sync()
.then(function() {
return cm.getConnection();
})
.then(function(connection) {
// Save current connection
conn = connection;

// simulate a unexpected end
connection.unwrap().emit('error', {code: 'ECONNRESET'});
})
.then(function() {
return cm.releaseConnection(conn);
})
.then(function() {
// Get next available connection
return cm.getConnection();
})
.then(function(connection) {
expect(conn).to.not.be.equal(connection);
expect(cm.validate(conn)).to.not.be.ok;

return cm.releaseConnection(connection);
});
});
});
}

0 comments on commit c94219c

Please sign in to comment.