2019-06-04 00:37:17 +02:00
|
|
|
const { isUndefined } = require('lodash');
|
2017-03-26 17:43:17 +02:00
|
|
|
|
2016-08-09 17:23:07 -04:00
|
|
|
const Promise = require('bluebird');
|
2016-06-20 17:03:52 +02:00
|
|
|
const Transaction = require('../../transaction');
|
|
|
|
const debugTx = require('debug')('knex:tx');
|
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = class Oracle_Transaction extends Transaction {
|
2016-06-20 17:03:52 +02:00
|
|
|
// disable autocommit to allow correct behavior (default is true)
|
2016-09-12 18:45:35 -04:00
|
|
|
begin() {
|
2016-06-20 17:03:52 +02:00
|
|
|
return Promise.resolve();
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
commit(conn, value) {
|
2016-06-20 17:03:52 +02:00
|
|
|
this._completed = true;
|
2018-07-09 08:10:34 -04:00
|
|
|
return conn
|
|
|
|
.commitAsync()
|
2016-06-20 17:03:52 +02:00
|
|
|
.return(value)
|
|
|
|
.then(this._resolver, this._rejecter);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
release(conn, value) {
|
2016-06-20 17:03:52 +02:00
|
|
|
return this._resolver(value);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rollback(conn, err) {
|
2016-06-20 17:03:52 +02:00
|
|
|
const self = this;
|
|
|
|
this._completed = true;
|
|
|
|
debugTx('%s: rolling back', this.txid);
|
2018-07-09 08:10:34 -04:00
|
|
|
return conn
|
|
|
|
.rollbackAsync()
|
|
|
|
.timeout(5000)
|
|
|
|
.catch(Promise.TimeoutError, function(e) {
|
|
|
|
self._rejecter(e);
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
if (isUndefined(err)) {
|
|
|
|
err = new Error(`Transaction rejected with non-error: ${err}`);
|
|
|
|
}
|
|
|
|
self._rejecter(err);
|
|
|
|
});
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
|
|
|
|
2016-10-14 17:00:39 +02:00
|
|
|
savepoint(conn) {
|
|
|
|
return this.query(conn, `SAVEPOINT ${this.txid}`);
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:45:35 -04:00
|
|
|
acquireConnection(config) {
|
2016-06-20 17:03:52 +02:00
|
|
|
const t = this;
|
|
|
|
return Promise.try(function() {
|
2016-10-09 14:00:55 -04:00
|
|
|
return t.client.acquireConnection().then(function(cnx) {
|
2018-02-21 13:26:59 +01:00
|
|
|
cnx.__knexTxId = t.txid;
|
2016-06-20 17:03:52 +02:00
|
|
|
cnx.isTransaction = true;
|
|
|
|
return cnx;
|
|
|
|
});
|
|
|
|
}).disposer(function(connection) {
|
|
|
|
debugTx('%s: releasing connection', t.txid);
|
|
|
|
connection.isTransaction = false;
|
2018-07-09 08:10:34 -04:00
|
|
|
connection.commitAsync().then(function(err) {
|
|
|
|
if (err) {
|
|
|
|
this._rejecter(err);
|
|
|
|
}
|
|
|
|
if (!config.connection) {
|
|
|
|
t.client.releaseConnection(connection);
|
|
|
|
} else {
|
|
|
|
debugTx('%s: not releasing external connection', t.txid);
|
|
|
|
}
|
|
|
|
});
|
2016-06-20 17:03:52 +02:00
|
|
|
});
|
|
|
|
}
|
2019-06-04 00:37:17 +02:00
|
|
|
};
|