2019-06-17 02:14:17 +02:00
|
|
|
const Bluebird = require('bluebird');
|
2019-06-04 00:37:17 +02:00
|
|
|
const Transaction = require('../../transaction');
|
|
|
|
|
const { isUndefined } = require('lodash');
|
2018-07-09 08:10:34 -04:00
|
|
|
const debugTx = require('debug')('knex:tx');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = class Oracle_Transaction extends Transaction {
|
2016-03-02 17:07:05 +01:00
|
|
|
// disable autocommit to allow correct behavior (default is true)
|
2016-05-17 01:01:34 +10:00
|
|
|
begin() {
|
2019-06-17 02:14:17 +02:00
|
|
|
return Bluebird.resolve();
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
commit(conn, value) {
|
2018-07-09 08:10:34 -04:00
|
|
|
this._completed = true;
|
|
|
|
|
return conn
|
|
|
|
|
.commitAsync()
|
2019-10-25 20:59:30 +03:00
|
|
|
.then(() => value)
|
2018-07-09 08:10:34 -04:00
|
|
|
.then(this._resolver, this._rejecter);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
release(conn, value) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return this._resolver(value);
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
rollback(conn, err) {
|
2018-07-09 08:10:34 -04:00
|
|
|
this._completed = true;
|
|
|
|
|
debugTx('%s: rolling back', this.txid);
|
|
|
|
|
return conn
|
|
|
|
|
.rollbackAsync()
|
2016-03-02 17:07:05 +01:00
|
|
|
.throw(err)
|
2017-03-26 17:43:17 +02:00
|
|
|
.catch((error) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (isUndefined(error)) {
|
2019-06-21 12:56:00 +02:00
|
|
|
if (this.doNotRejectOnRollback) {
|
|
|
|
|
this._resolver();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
error = new Error(`Transaction rejected with non-error: ${error}`);
|
2017-03-26 17:43:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._rejecter(error);
|
|
|
|
|
});
|
2016-09-12 18:45:35 -04:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2019-11-28 22:44:18 +03:00
|
|
|
acquireConnection(config, cb) {
|
2019-11-28 00:01:29 +01:00
|
|
|
const configConnection = config && config.connection;
|
2019-06-17 02:14:17 +02:00
|
|
|
return new Bluebird((resolve, reject) => {
|
2019-06-07 17:30:39 -04:00
|
|
|
try {
|
2019-11-28 22:44:18 +03:00
|
|
|
resolve(configConnection || this.client.acquireConnection());
|
2019-06-07 17:30:39 -04:00
|
|
|
} catch (e) {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-07-09 08:10:34 -04:00
|
|
|
.then((connection) => {
|
2018-02-21 13:26:59 +01:00
|
|
|
connection.__knexTxId = this.txid;
|
|
|
|
|
|
|
|
|
|
return connection;
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
2019-06-17 07:00:31 -04:00
|
|
|
.then((connection) => {
|
2019-11-28 22:44:18 +03:00
|
|
|
if (!this.outerTx) {
|
2018-07-09 08:10:34 -04:00
|
|
|
connection.setAutoCommit(false);
|
2018-02-21 13:26:59 +01:00
|
|
|
}
|
2019-06-17 07:00:31 -04:00
|
|
|
return connection;
|
2018-07-09 08:10:34 -04:00
|
|
|
})
|
2019-11-28 22:44:18 +03:00
|
|
|
.then(async (connection) => {
|
|
|
|
|
try {
|
|
|
|
|
return await cb(connection);
|
|
|
|
|
} finally {
|
|
|
|
|
debugTx('%s: releasing connection', this.txid);
|
|
|
|
|
connection.setAutoCommit(true);
|
|
|
|
|
if (!configConnection) {
|
|
|
|
|
this.client.releaseConnection(connection);
|
|
|
|
|
} else {
|
|
|
|
|
debugTx('%s: not releasing external connection', this.txid);
|
|
|
|
|
}
|
2018-02-21 13:26:59 +01:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2019-06-04 00:37:17 +02:00
|
|
|
};
|