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

Commit 303b453a by Andy Edwards Committed by GitHub

refactor(transaction): asyncify methods (#12156)

1 parent 80de9f60
Showing with 44 additions and 42 deletions
......@@ -64,7 +64,7 @@ class Transaction {
} finally {
this.finished = 'commit';
if (!this.parent) {
await this.cleanup();
this.cleanup();
}
for (const hook of this._afterCommitHooks) {
await hook.apply(this, [this]);
......@@ -77,30 +77,30 @@ class Transaction {
*
* @returns {Promise}
*/
rollback() {
async rollback() {
if (this.finished) {
return Promise.reject(new Error(`Transaction cannot be rolled back because it has been finished with state: ${this.finished}`));
throw new Error(`Transaction cannot be rolled back because it has been finished with state: ${this.finished}`);
}
if (!this.connection) {
return Promise.reject(new Error('Transaction cannot be rolled back because it never started'));
throw new Error('Transaction cannot be rolled back because it never started');
}
this._clearCls();
return this
.sequelize
.getQueryInterface()
.rollbackTransaction(this, this.options)
.finally(() => {
if (!this.parent) {
return this.cleanup();
}
return this;
});
try {
return await this
.sequelize
.getQueryInterface()
.rollbackTransaction(this, this.options);
} finally {
if (!this.parent) {
this.cleanup();
}
}
}
prepareEnvironment(useCLS) {
async prepareEnvironment(useCLS) {
let connectionPromise;
if (useCLS === undefined) {
......@@ -117,47 +117,49 @@ class Transaction {
connectionPromise = this.sequelize.connectionManager.getConnection(acquireOptions);
}
return connectionPromise
.then(connection => {
this.connection = connection;
this.connection.uuid = this.id;
})
.then(() => {
return this.begin()
.then(() => this.setDeferrable())
.catch(setupErr => this.rollback().finally(() => {
throw setupErr;
}));
})
.then(result => {
if (useCLS && this.sequelize.constructor._cls) {
this.sequelize.constructor._cls.set('transaction', this);
}
return Promise.resolve(null).then(() => result);
});
let result;
const connection = await connectionPromise;
this.connection = connection;
this.connection.uuid = this.id;
try {
await this.begin();
result = await this.setDeferrable();
} catch (setupErr) {
try {
result = await this.rollback();
} finally {
throw setupErr; // eslint-disable-line no-unsafe-finally
}
}
if (useCLS && this.sequelize.constructor._cls) {
this.sequelize.constructor._cls.set('transaction', this);
}
return result;
}
setDeferrable() {
async setDeferrable() {
if (this.options.deferrable) {
return this
return await this
.sequelize
.getQueryInterface()
.deferConstraints(this, this.options);
}
}
begin() {
async begin() {
const queryInterface = this.sequelize.getQueryInterface();
if ( this.sequelize.dialect.supports.settingIsolationLevelDuringTransaction ) {
return queryInterface.startTransaction(this, this.options).then(() => {
return queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options);
});
await queryInterface.startTransaction(this, this.options);
return queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options);
}
return queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options).then(() => {
return queryInterface.startTransaction(this, this.options);
});
await queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options);
return queryInterface.startTransaction(this, this.options);
}
cleanup() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!