2020-12-28 16:55:08 +02:00
|
|
|
const Transaction = require('../../execution/transaction');
|
2018-07-09 08:10:34 -04:00
|
|
|
const debug = require('debug')('knex:tx');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = class Transaction_MSSQL extends Transaction {
|
2016-05-17 01:01:34 +10:00
|
|
|
begin(conn) {
|
2018-07-09 08:10:34 -04:00
|
|
|
debug('%s: begin', this.txid);
|
2020-12-30 16:13:35 +00:00
|
|
|
return conn.tx_
|
|
|
|
.begin(nameToIsolationLevelEnum(this.isolationLevel))
|
|
|
|
.then(this._resolver, this._rejecter);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2020-02-26 00:50:24 +03:00
|
|
|
async savepoint(conn) {
|
2018-07-09 08:10:34 -04:00
|
|
|
debug('%s: savepoint at', this.txid);
|
2020-02-26 00:50:24 +03:00
|
|
|
return this.query(conn, `SAVE TRANSACTION ${this.txid}`);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
commit(conn, value) {
|
2018-07-09 08:10:34 -04:00
|
|
|
this._completed = true;
|
|
|
|
debug('%s: commit', this.txid);
|
|
|
|
return conn.tx_.commit().then(() => this._resolver(value), this._rejecter);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
release(conn, value) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return this._resolver(value);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
rollback(conn, error) {
|
2018-07-09 08:10:34 -04:00
|
|
|
this._completed = true;
|
|
|
|
debug('%s: rolling back', this.txid);
|
|
|
|
return conn.tx_.rollback().then(
|
|
|
|
() => {
|
|
|
|
let err = error;
|
2020-04-18 20:41:23 +03:00
|
|
|
if (error === undefined) {
|
2019-06-21 12:56:00 +02:00
|
|
|
if (this.doNotRejectOnRollback) {
|
|
|
|
this._resolver();
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
err = new Error(`Transaction rejected with non-error: ${error}`);
|
2017-01-30 16:00:03 -05:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
this._rejecter(err);
|
|
|
|
},
|
|
|
|
(err) => {
|
2020-12-11 23:56:14 +02:00
|
|
|
if (error) {
|
|
|
|
try {
|
|
|
|
err.originalError = error;
|
|
|
|
} catch (_err) {
|
|
|
|
// This is to handle https://github.com/knex/knex/issues/4128
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
return this._rejecter(err);
|
|
|
|
}
|
|
|
|
);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2020-02-26 00:50:24 +03:00
|
|
|
async rollbackTo(conn, error) {
|
2018-07-09 08:10:34 -04:00
|
|
|
debug('%s: rolling backTo', this.txid);
|
2020-02-26 00:50:24 +03:00
|
|
|
await this.query(conn, `ROLLBACK TRANSACTION ${this.txid}`, 2, error);
|
|
|
|
|
|
|
|
this._rejecter(error);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Acquire a connection and create a disposer - either using the one passed
|
|
|
|
// via config or getting one off the client. The disposer will be called once
|
|
|
|
// the original promise is marked completed.
|
2020-03-06 19:15:23 -05:00
|
|
|
async acquireConnection(config, cb) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const configConnection = config && config.connection;
|
2020-03-06 19:15:23 -05:00
|
|
|
const conn =
|
|
|
|
(this.outerTx && this.outerTx.conn) ||
|
|
|
|
configConnection ||
|
|
|
|
(await this.client.acquireConnection());
|
|
|
|
|
|
|
|
try {
|
|
|
|
conn.__knexTxId = this.txid;
|
|
|
|
if (!this.outerTx) {
|
|
|
|
this.conn = conn;
|
|
|
|
conn.tx_ = conn.transaction();
|
2019-06-07 17:30:39 -04:00
|
|
|
}
|
2020-03-06 19:15:23 -05:00
|
|
|
|
|
|
|
return await cb(conn);
|
|
|
|
} finally {
|
|
|
|
if (!this.outerTx) {
|
|
|
|
if (conn.tx_) {
|
|
|
|
if (!this._completed) {
|
|
|
|
debug('%s: unreleased transaction', this.txid);
|
|
|
|
conn.tx_.rollback();
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
2020-03-06 19:15:23 -05:00
|
|
|
conn.tx_ = null;
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
2020-03-06 19:15:23 -05:00
|
|
|
this.conn = null;
|
|
|
|
if (!configConnection) {
|
|
|
|
debug('%s: releasing connection', this.txid);
|
|
|
|
this.client.releaseConnection(conn);
|
|
|
|
} else {
|
|
|
|
debug('%s: not releasing external connection', this.txid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2019-06-04 00:37:17 +02:00
|
|
|
};
|
2020-12-30 16:13:35 +00:00
|
|
|
|
|
|
|
function nameToIsolationLevelEnum(level) {
|
|
|
|
if (!level) return;
|
|
|
|
level = level.toUpperCase().replace(' ', '_');
|
|
|
|
const knownEnum = isolationEnum[level];
|
|
|
|
if (!knownEnum) {
|
|
|
|
throw new Error(
|
|
|
|
`Unknown Isolation level, was expecting one of: ${JSON.stringify(
|
|
|
|
humanReadableKeys
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return knownEnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Based on: https://github.com/tediousjs/node-mssql/blob/master/lib/isolationlevel.js
|
|
|
|
const isolationEnum = {
|
|
|
|
READ_UNCOMMITTED: 0x01,
|
|
|
|
READ_COMMITTED: 0x02,
|
|
|
|
REPEATABLE_READ: 0x03,
|
|
|
|
SERIALIZABLE: 0x04,
|
|
|
|
SNAPSHOT: 0x05,
|
|
|
|
};
|
|
|
|
const humanReadableKeys = Object.keys(isolationEnum).map((key) =>
|
|
|
|
key.toLowerCase().replace('_', ' ')
|
|
|
|
);
|