transaction.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  4. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  5. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  6. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  7. var _inherits2 = require('babel-runtime/helpers/inherits');
  8. var _inherits3 = _interopRequireDefault(_inherits2);
  9. var _bluebird = require('bluebird');
  10. var _bluebird2 = _interopRequireDefault(_bluebird);
  11. var _transaction = require('../../transaction');
  12. var _transaction2 = _interopRequireDefault(_transaction);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. var debug = require('debug')('knex:tx');
  15. var Transaction_MSSQL = function (_Transaction) {
  16. (0, _inherits3.default)(Transaction_MSSQL, _Transaction);
  17. function Transaction_MSSQL() {
  18. (0, _classCallCheck3.default)(this, Transaction_MSSQL);
  19. return (0, _possibleConstructorReturn3.default)(this, _Transaction.apply(this, arguments));
  20. }
  21. Transaction_MSSQL.prototype.begin = function begin(conn) {
  22. debug('%s: begin', this.txid);
  23. return conn.tx_.begin().then(this._resolver, this._rejecter);
  24. };
  25. Transaction_MSSQL.prototype.savepoint = function savepoint(conn) {
  26. var _this2 = this;
  27. debug('%s: savepoint at', this.txid);
  28. return _bluebird2.default.resolve().then(function () {
  29. return _this2.query(conn, 'SAVE TRANSACTION ' + _this2.txid);
  30. });
  31. };
  32. Transaction_MSSQL.prototype.commit = function commit(conn, value) {
  33. var _this3 = this;
  34. this._completed = true;
  35. debug('%s: commit', this.txid);
  36. return conn.tx_.commit().then(function () {
  37. return _this3._resolver(value);
  38. }, this._rejecter);
  39. };
  40. Transaction_MSSQL.prototype.release = function release(conn, value) {
  41. return this._resolver(value);
  42. };
  43. Transaction_MSSQL.prototype.rollback = function rollback(conn, error) {
  44. var _this4 = this;
  45. this._completed = true;
  46. debug('%s: rolling back', this.txid);
  47. return conn.tx_.rollback().then(function () {
  48. return _this4._rejecter(error);
  49. }, function (err) {
  50. if (error) err.originalError = error;
  51. return _this4._rejecter(err);
  52. });
  53. };
  54. Transaction_MSSQL.prototype.rollbackTo = function rollbackTo(conn, error) {
  55. var _this5 = this;
  56. debug('%s: rolling backTo', this.txid);
  57. return _bluebird2.default.resolve().then(function () {
  58. return _this5.query(conn, 'ROLLBACK TRANSACTION ' + _this5.txid, 2, error);
  59. }).then(function () {
  60. return _this5._rejecter(error);
  61. });
  62. };
  63. // Acquire a connection and create a disposer - either using the one passed
  64. // via config or getting one off the client. The disposer will be called once
  65. // the original promise is marked completed.
  66. Transaction_MSSQL.prototype.acquireConnection = function acquireConnection(config) {
  67. var t = this;
  68. var configConnection = config && config.connection;
  69. return _bluebird2.default.try(function () {
  70. return (t.outerTx ? t.outerTx.conn : null) || configConnection || t.client.acquireConnection();
  71. }).tap(function (conn) {
  72. if (!t.outerTx) {
  73. t.conn = conn;
  74. conn.tx_ = conn.transaction();
  75. }
  76. }).disposer(function (conn) {
  77. if (t.outerTx) return;
  78. if (conn.tx_) {
  79. if (!t._completed) {
  80. debug('%s: unreleased transaction', t.txid);
  81. conn.tx_.rollback();
  82. }
  83. conn.tx_ = null;
  84. }
  85. t.conn = null;
  86. if (!configConnection) {
  87. debug('%s: releasing connection', t.txid);
  88. t.client.releaseConnection(conn);
  89. } else {
  90. debug('%s: not releasing external connection', t.txid);
  91. }
  92. });
  93. };
  94. return Transaction_MSSQL;
  95. }(_transaction2.default);
  96. exports.default = Transaction_MSSQL;
  97. module.exports = exports['default'];