transaction.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. var Promise = require('bluebird');
  11. var Transaction = require('../../transaction');
  12. var debugTx = require('debug')('knex:tx');
  13. var Oracle_Transaction = function (_Transaction) {
  14. (0, _inherits3.default)(Oracle_Transaction, _Transaction);
  15. function Oracle_Transaction() {
  16. (0, _classCallCheck3.default)(this, Oracle_Transaction);
  17. return (0, _possibleConstructorReturn3.default)(this, _Transaction.apply(this, arguments));
  18. }
  19. // disable autocommit to allow correct behavior (default is true)
  20. Oracle_Transaction.prototype.begin = function begin() {
  21. return Promise.resolve();
  22. };
  23. Oracle_Transaction.prototype.commit = function commit(conn, value) {
  24. this._completed = true;
  25. return conn.commitAsync().return(value).then(this._resolver, this._rejecter);
  26. };
  27. Oracle_Transaction.prototype.release = function release(conn, value) {
  28. return this._resolver(value);
  29. };
  30. Oracle_Transaction.prototype.rollback = function rollback(conn, err) {
  31. var self = this;
  32. this._completed = true;
  33. debugTx('%s: rolling back', this.txid);
  34. return conn.rollbackAsync().timeout(5000).catch(Promise.TimeoutError, function (e) {
  35. self._rejecter(e);
  36. }).then(function () {
  37. self._rejecter(err);
  38. });
  39. };
  40. Oracle_Transaction.prototype.savepoint = function savepoint(conn) {
  41. return this.query(conn, 'SAVEPOINT ' + this.txid);
  42. };
  43. Oracle_Transaction.prototype.acquireConnection = function acquireConnection(config) {
  44. var t = this;
  45. return Promise.try(function () {
  46. return t.client.acquireConnection().then(function (cnx) {
  47. cnx.isTransaction = true;
  48. return cnx;
  49. });
  50. }).disposer(function (connection) {
  51. debugTx('%s: releasing connection', t.txid);
  52. connection.isTransaction = false;
  53. connection.commitAsync().then(function (err) {
  54. if (err) {
  55. this._rejecter(err);
  56. }
  57. if (!config.connection) {
  58. t.client.releaseConnection(connection);
  59. } else {
  60. debugTx('%s: not releasing external connection', t.txid);
  61. }
  62. });
  63. });
  64. };
  65. return Oracle_Transaction;
  66. }(Transaction);
  67. exports.default = Oracle_Transaction;
  68. module.exports = exports['default'];