knex/lib/dialects/mssql/transaction.js

98 lines
2.6 KiB
JavaScript
Raw Normal View History

const Transaction = require('../../transaction');
const debug = require('debug')('knex:tx');
2016-03-02 17:07:05 +01:00
module.exports = class Transaction_MSSQL extends Transaction {
begin(conn) {
debug('%s: begin', this.txid);
return conn.tx_.begin().then(this._resolver, this._rejecter);
2016-09-12 18:45:35 -04:00
}
2016-03-02 17:07:05 +01:00
async savepoint(conn) {
debug('%s: savepoint at', this.txid);
return this.query(conn, `SAVE TRANSACTION ${this.txid}`);
2016-09-12 18:45:35 -04:00
}
2016-03-02 17:07:05 +01:00
commit(conn, value) {
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
release(conn, value) {
return this._resolver(value);
2016-09-12 18:45:35 -04:00
}
2016-03-02 17:07:05 +01:00
rollback(conn, error) {
this._completed = true;
debug('%s: rolling back', this.txid);
return conn.tx_.rollback().then(
() => {
let err = error;
if (error === undefined) {
if (this.doNotRejectOnRollback) {
this._resolver();
return;
}
err = new Error(`Transaction rejected with non-error: ${error}`);
}
this._rejecter(err);
},
(err) => {
if (error) {
try {
err.originalError = error;
} catch (_err) {
// This is to handle https://github.com/knex/knex/issues/4128
}
}
return this._rejecter(err);
}
);
2016-09-12 18:45:35 -04:00
}
2016-03-02 17:07:05 +01:00
async rollbackTo(conn, error) {
debug('%s: rolling backTo', this.txid);
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.
async acquireConnection(config, cb) {
const configConnection = config && config.connection;
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
}
return await cb(conn);
} finally {
if (!this.outerTx) {
if (conn.tx_) {
if (!this._completed) {
debug('%s: unreleased transaction', this.txid);
conn.tx_.rollback();
}
conn.tx_ = null;
}
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
}
};