knex/lib/dialects/oracledb/transaction.js

87 lines
2.2 KiB
JavaScript
Raw Normal View History

const { isUndefined } = require('lodash');
const Bluebird = require('bluebird');
const Transaction = require('../../transaction');
const debugTx = require('debug')('knex:tx');
module.exports = class Oracle_Transaction extends Transaction {
// disable autocommit to allow correct behavior (default is true)
2016-09-12 18:45:35 -04:00
begin() {
return Bluebird.resolve();
2016-09-12 18:45:35 -04:00
}
commit(conn, value) {
this._completed = true;
return conn
.commitAsync()
.then(() => value)
.then(this._resolver, this._rejecter);
2016-09-12 18:45:35 -04:00
}
release(conn, value) {
return this._resolver(value);
2016-09-12 18:45:35 -04:00
}
rollback(conn, err) {
const self = this;
this._completed = true;
debugTx('%s: rolling back', this.txid);
return conn
.rollbackAsync()
.timeout(5000)
.catch(Bluebird.TimeoutError, function(e) {
self._rejecter(e);
})
.then(function() {
if (isUndefined(err)) {
if (self.doNotRejectOnRollback) {
self._resolver();
return;
}
err = new Error(`Transaction rejected with non-error: ${err}`);
}
self._rejecter(err);
});
2016-09-12 18:45:35 -04:00
}
savepoint(conn) {
return this.query(conn, `SAVEPOINT ${this.txid}`);
}
2019-11-28 22:44:18 +03:00
acquireConnection(config, cb) {
const configConnection = config && config.connection;
const t = this;
2019-11-28 22:44:18 +03:00
return new Bluebird((resolve, reject) => {
2019-06-07 17:30:39 -04:00
try {
2019-11-28 22:44:18 +03:00
this.client
2019-06-07 17:30:39 -04:00
.acquireConnection()
2019-11-28 22:44:18 +03:00
.then((cnx) => {
cnx.__knexTxId = this.txid;
2019-06-07 17:30:39 -04:00
cnx.isTransaction = true;
resolve(cnx);
})
.catch(reject);
} catch (e) {
reject(e);
}
2019-11-28 22:44:18 +03:00
}).then(async (connection) => {
try {
return await cb(connection);
} finally {
debugTx('%s: releasing connection', this.txid);
connection.isTransaction = false;
connection.commitAsync().then(function(err) {
if (err) {
this._rejecter(err);
}
if (!configConnection) {
t.client.releaseConnection(connection);
} else {
debugTx('%s: not releasing external connection', t.txid);
}
});
}
});
}
};