2015-04-22 15:39:29 -04:00
|
|
|
'use strict';
|
2015-04-27 15:58:48 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var inherits = require('inherits');
|
|
|
|
var Promise = require('../../promise');
|
|
|
|
var Transaction = require('../../transaction');
|
|
|
|
var assign = require('lodash/object/assign');
|
|
|
|
var debugTx = require('debug')('knex:tx');
|
2015-04-22 15:24:29 -04:00
|
|
|
|
2015-04-23 14:51:33 -04:00
|
|
|
function Oracle_Transaction(client, container, config, outerTx) {
|
2015-05-09 13:58:18 -04:00
|
|
|
Transaction.call(this, client, container, config, outerTx);
|
2014-08-11 12:25:39 +02:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
inherits(Oracle_Transaction, Transaction);
|
2014-08-11 12:25:39 +02:00
|
|
|
|
2015-04-22 15:24:29 -04:00
|
|
|
assign(Oracle_Transaction.prototype, {
|
|
|
|
|
|
|
|
// disable autocommit to allow correct behavior (default is true)
|
2015-05-09 13:58:18 -04:00
|
|
|
begin: function begin() {
|
|
|
|
return Promise.resolve();
|
2015-04-22 15:24:29 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
commit: function commit(conn, value) {
|
|
|
|
this._completed = true;
|
|
|
|
return conn.commitAsync()['return'](value).then(this._resolver, this._rejecter);
|
2015-04-22 15:24:29 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
release: function release(conn, value) {
|
|
|
|
return this._resolver(value);
|
|
|
|
},
|
2015-04-27 20:22:05 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
rollback: function rollback(conn, err) {
|
|
|
|
this._completed = true;
|
|
|
|
debugTx('%s: rolling back', this.txid);
|
|
|
|
return conn.rollbackAsync()['throw'](err)['catch'](this._rejecter);
|
2015-04-22 15:24:29 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
acquireConnection: function acquireConnection(config) {
|
|
|
|
var t = this;
|
|
|
|
return Promise['try'](function () {
|
|
|
|
return config.connection || t.client.acquireConnection();
|
|
|
|
}).tap(function (connection) {
|
2015-04-27 20:22:05 -04:00
|
|
|
if (!t.outerTx) {
|
2015-05-09 13:58:18 -04:00
|
|
|
connection.setAutoCommit(false);
|
2015-04-22 15:24:29 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
}).disposer(function (connection) {
|
|
|
|
debugTx('%s: releasing connection', t.txid);
|
|
|
|
connection.setAutoCommit(true);
|
2015-04-22 15:24:29 -04:00
|
|
|
if (!config.connection) {
|
2015-05-09 13:58:18 -04:00
|
|
|
t.client.releaseConnection(connection);
|
2015-04-22 15:24:29 -04:00
|
|
|
} else {
|
2015-05-09 13:58:18 -04:00
|
|
|
debugTx('%s: not releasing external connection', t.txid);
|
2015-04-22 15:24:29 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2015-04-22 15:24:29 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2015-04-22 15:24:29 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
module.exports = Oracle_Transaction;
|