2019-06-04 00:37:17 +02:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const Transaction = require('../../transaction');
|
|
|
|
const { isUndefined } = require('lodash');
|
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);
|
|
|
|
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
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
savepoint(conn) {
|
2018-07-09 08:10:34 -04:00
|
|
|
debug('%s: savepoint at', this.txid);
|
|
|
|
return Promise.resolve().then(() =>
|
|
|
|
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;
|
|
|
|
if (isUndefined(error)) {
|
|
|
|
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) => {
|
|
|
|
if (error) err.originalError = error;
|
|
|
|
return this._rejecter(err);
|
|
|
|
}
|
|
|
|
);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
rollbackTo(conn, error) {
|
2018-07-09 08:10:34 -04:00
|
|
|
debug('%s: rolling backTo', this.txid);
|
2016-03-02 17:07:05 +01:00
|
|
|
return Promise.resolve()
|
2018-07-09 08:10:34 -04:00
|
|
|
.then(() =>
|
|
|
|
this.query(conn, `ROLLBACK TRANSACTION ${this.txid}`, 2, error)
|
|
|
|
)
|
|
|
|
.then(() => 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.
|
2016-05-17 01:01:34 +10:00
|
|
|
acquireConnection(config) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const t = this;
|
|
|
|
const configConnection = config && config.connection;
|
2016-05-17 01:01:34 +10:00
|
|
|
return Promise.try(() => {
|
2018-07-09 08:10:34 -04:00
|
|
|
return (
|
|
|
|
(t.outerTx ? t.outerTx.conn : null) ||
|
2016-05-17 01:01:34 +10:00
|
|
|
configConnection ||
|
2018-07-09 08:10:34 -04:00
|
|
|
t.client.acquireConnection()
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
})
|
2018-07-09 08:10:34 -04:00
|
|
|
.tap(function(conn) {
|
|
|
|
conn.__knexTxId = t.txid;
|
|
|
|
if (!t.outerTx) {
|
|
|
|
t.conn = conn;
|
|
|
|
conn.tx_ = conn.transaction();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.disposer(function(conn) {
|
|
|
|
if (t.outerTx) return;
|
|
|
|
if (conn.tx_) {
|
|
|
|
if (!t._completed) {
|
|
|
|
debug('%s: unreleased transaction', t.txid);
|
|
|
|
conn.tx_.rollback();
|
|
|
|
}
|
|
|
|
conn.tx_ = null;
|
|
|
|
}
|
|
|
|
t.conn = null;
|
|
|
|
if (!configConnection) {
|
|
|
|
debug('%s: releasing connection', t.txid);
|
|
|
|
t.client.releaseConnection(conn);
|
|
|
|
} else {
|
|
|
|
debug('%s: not releasing external connection', t.txid);
|
|
|
|
}
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2019-06-04 00:37:17 +02:00
|
|
|
};
|