Skip to content

Commit

Permalink
Merge pull request sequelize#5428 from jharting/more-debug-when-trans…
Browse files Browse the repository at this point in the history
…action-finished

Log which query tried to run on a finished transaction
  • Loading branch information
mickhansen committed Feb 21, 2016
2 parents 34b964b + 0d0aad6 commit 5479722
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,9 @@ Sequelize.prototype.query = function(sql, options) {
}

if (options.transaction && options.transaction.finished) {
return Promise.reject(new Error(options.transaction.finished+' has been called on this transaction('+options.transaction.id+'), you can no longer use it'));
var error = new Error(options.transaction.finished+' has been called on this transaction('+options.transaction.id+'), you can no longer use it. (The rejected query is attached as the \'sql\' property of this error)');
error.sql = sql;
return Promise.reject(error);
}

if (this.test.$trackRunningQueries) {
Expand Down
34 changes: 23 additions & 11 deletions test/integration/transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,17 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {

it('does not allow queries after commit', function() {
var self = this;
return expect(
this.sequelize.transaction().then(function(t) {
return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true}).then(function() {
return t.commit();
}).then(function() {
return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true});
});
})
).to.eventually.be.rejected;
return this.sequelize.transaction().then(function(t) {
return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true}).then(function() {
return t.commit();
}).then(function() {
return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true});
});
}).throw(new Error('Expected error not thrown'))
.catch (function (err) {
expect (err.message).to.match(/commit has been called on this transaction\([^)]+\), you can no longer use it\. \(The rejected query is attached as the 'sql' property of this error\)/);
expect (err.sql).to.equal('SELECT 1+1');
});
});

it('does not allow queries immediatly after commit call', function() {
Expand All @@ -135,7 +137,12 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {
return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true}).then(function() {
return Promise.join(
expect(t.commit()).to.eventually.be.fulfilled,
expect(self.sequelize.query('SELECT 1+1', {transaction: t, raw: true})).to.eventually.be.rejectedWith(/commit has been called on this transaction\([^)]+\), you can no longer use it/)
self.sequelize.query('SELECT 1+1', {transaction: t, raw: true})
.throw(new Error('Expected error not thrown'))
.catch (function (err) {
expect (err.message).to.match(/commit has been called on this transaction\([^)]+\), you can no longer use it\. \(The rejected query is attached as the 'sql' property of this error\)/);
expect (err.sql).to.equal('SELECT 1+1');
})
);
});
})
Expand All @@ -161,7 +168,12 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {
this.sequelize.transaction().then(function(t) {
return Promise.join(
expect(t.rollback()).to.eventually.be.fulfilled,
expect(self.sequelize.query('SELECT 1+1', {transaction: t, raw: true})).to.eventually.be.rejectedWith(/rollback has been called on this transaction\([^)]+\), you can no longer use it/)
self.sequelize.query('SELECT 1+1', {transaction: t, raw: true})
.throw(new Error('Expected error not thrown'))
.catch (function (err) {
expect (err.message).to.match(/rollback has been called on this transaction\([^)]+\), you can no longer use it\. \(The rejected query is attached as the 'sql' property of this error\)/);
expect (err.sql).to.equal('SELECT 1+1');
})
);
})
).to.eventually.be.fulfilled;
Expand Down

0 comments on commit 5479722

Please sign in to comment.