不要怂,就是干,撸起袖子干!

Commit 1e0d9a86 by 木士羽 Committed by Sushant

fix(sequelize/query): release connection after all retries (#8247)

1 parent fdf3e99e
Showing with 41 additions and 2 deletions
......@@ -528,12 +528,14 @@ class Sequelize {
return options.transaction ? options.transaction.connection : this.connectionManager.getConnection(options);
}).then(connection => {
const query = new this.dialect.Query(connection, this, options);
const retryOptions = Utils._.extend(this.options.retry, options.retry || {});
return retry(() => query.run(sql, bindParameters).finally(() => {
return retry(() => query.run(sql, bindParameters), retryOptions)
.finally(() => {
if (!options.transaction) {
return this.connectionManager.releaseConnection(connection);
}
}), Utils._.extend(this.options.retry, options.retry || {}));
});
}).finally(() => {
if (this.test._trackRunningQueries) {
this.test._runningQueries--;
......
'use strict';
const chai = require('chai'),
sinon = require('sinon'),
expect = chai.expect,
Support = require(__dirname + '/support'),
Sequelize = Support.Sequelize,
Promise = Sequelize.Promise,
current = Support.sequelize;
describe('sequelize.query', () => {
it('connection should be released only once when retry fails', () => {
const getConnectionStub = sinon.stub(current.connectionManager, 'getConnection', () => {
return Promise.resolve({});
});
const releaseConnectionStub = sinon.stub(current.connectionManager, 'releaseConnection', () => {
return Promise.resolve();
});
const queryStub = sinon.stub(current.dialect.Query.prototype, 'run', () => {
return Promise.reject(new Error('wrong sql'));
});
return current.query('THIS IS A WRONG SQL', {
retry: {
max: 2,
// retry for all errors
match: null
}
})
.catch(() => {})
.finally(() => {
expect(releaseConnectionStub).have.been.calledOnce;
queryStub.restore();
getConnectionStub.restore();
releaseConnectionStub.restore();
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!