2015-12-08 11:37:31 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var inherits = require('inherits');
|
2015-12-09 17:53:53 -06:00
|
|
|
var assign = require('lodash/object/assign');
|
|
|
|
|
var Promise = require('../../promise');
|
|
|
|
|
var Transaction = require('../../transaction');
|
|
|
|
|
var debug = require('debug')('knex:tx');
|
2015-12-08 11:37:31 -06:00
|
|
|
|
|
|
|
|
function Transaction_MSSQL() {
|
|
|
|
|
Transaction.apply(this, arguments);
|
|
|
|
|
}
|
|
|
|
|
inherits(Transaction_MSSQL, Transaction);
|
|
|
|
|
|
|
|
|
|
assign(Transaction_MSSQL.prototype, {
|
|
|
|
|
|
2015-12-09 17:53:53 -06:00
|
|
|
begin: function begin(conn) {
|
2015-12-10 13:35:37 -06:00
|
|
|
debug('%s: begin', this.txid);
|
2015-12-09 17:53:53 -06:00
|
|
|
return conn.tx_.begin().then(this._resolver, this._rejecter);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
savepoint: function savepoint(conn) {
|
2015-12-10 13:35:37 -06:00
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
|
|
debug('%s: savepoint at', this.txid);
|
|
|
|
|
return Promise.resolve().then(function () {
|
|
|
|
|
return _this.query(conn, 'SAVE TRANSACTION ' + _this.txid);
|
|
|
|
|
});
|
2015-12-09 17:53:53 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
commit: function commit(conn, value) {
|
2015-12-10 13:35:37 -06:00
|
|
|
var _this2 = this;
|
|
|
|
|
|
2015-12-09 17:53:53 -06:00
|
|
|
this._completed = true;
|
2015-12-10 13:35:37 -06:00
|
|
|
debug('%s: commit', this.txid);
|
|
|
|
|
return conn.tx_.commit().then(function () {
|
|
|
|
|
return _this2._resolver(value);
|
|
|
|
|
}, this._rejecter);
|
2015-12-09 17:53:53 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
release: function release(conn, value) {
|
|
|
|
|
return this._resolver(value);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
rollback: function rollback(conn, error) {
|
2015-12-10 13:35:37 -06:00
|
|
|
var _this3 = this;
|
|
|
|
|
|
2015-12-09 17:53:53 -06:00
|
|
|
this._completed = true;
|
|
|
|
|
debug('%s: rolling back', this.txid);
|
2015-12-10 13:35:37 -06:00
|
|
|
return conn.tx_.rollback().then(function () {
|
|
|
|
|
return _this3._rejecter(error);
|
|
|
|
|
});
|
2015-12-09 17:53:53 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
rollbackTo: function rollbackTo(conn, error) {
|
2015-12-10 13:35:37 -06:00
|
|
|
var _this4 = this;
|
|
|
|
|
|
|
|
|
|
debug('%s: rolling backTo', this.txid);
|
|
|
|
|
return Promise.resolve().then(function () {
|
|
|
|
|
return _this4.query(conn, 'ROLLBACK TRANSACTION ' + _this4.txid, 2, error);
|
|
|
|
|
}).then(function () {
|
|
|
|
|
return _this4._rejecter(error);
|
|
|
|
|
});
|
2015-12-09 17:53:53 -06: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.
|
|
|
|
|
acquireConnection: function acquireConnection(config) {
|
2015-12-08 11:37:31 -06:00
|
|
|
var t = this;
|
2015-12-09 17:53:53 -06:00
|
|
|
var configConnection = config && config.connection;
|
|
|
|
|
return Promise['try'](function () {
|
2015-12-10 13:35:37 -06:00
|
|
|
return (t.outerTx ? t.outerTx.conn : null) || configConnection || t.client.acquireConnection();
|
|
|
|
|
}).tap(function (conn) {
|
2015-12-09 17:53:53 -06:00
|
|
|
if (!t.outerTx) {
|
2015-12-10 13:35:37 -06:00
|
|
|
t.conn = conn;
|
|
|
|
|
conn.tx_ = conn.transaction();
|
2015-12-09 17:53:53 -06:00
|
|
|
}
|
2015-12-10 13:35:37 -06:00
|
|
|
}).disposer(function (conn) {
|
|
|
|
|
if (t.outerTx) return;
|
|
|
|
|
if (conn.tx_) {
|
2015-12-09 17:53:53 -06:00
|
|
|
if (!t._completed) {
|
|
|
|
|
debug('%s: unreleased transaction', t.txid);
|
2015-12-10 13:35:37 -06:00
|
|
|
conn.tx_.rollback();
|
2015-12-09 17:53:53 -06:00
|
|
|
}
|
2015-12-10 13:35:37 -06:00
|
|
|
conn.tx_ = null;
|
2015-12-09 17:53:53 -06:00
|
|
|
}
|
2015-12-10 13:35:37 -06:00
|
|
|
t.conn = null;
|
2015-12-09 17:53:53 -06:00
|
|
|
if (!configConnection) {
|
|
|
|
|
debug('%s: releasing connection', t.txid);
|
2015-12-10 13:35:37 -06:00
|
|
|
t.client.releaseConnection(conn);
|
2015-12-09 17:53:53 -06:00
|
|
|
} else {
|
|
|
|
|
debug('%s: not releasing external connection', t.txid);
|
|
|
|
|
}
|
2015-12-08 11:37:31 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = Transaction_MSSQL;
|