knex/src/dialects/oracle/transaction.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

import Promise from 'bluebird';
import Transaction from '../../transaction';
import { isUndefined } from 'lodash';
const debugTx = require('debug')('knex:tx');
2016-03-02 17:07:05 +01:00
2016-09-12 18:45:35 -04:00
export default class Oracle_Transaction extends Transaction {
2016-03-02 17:07:05 +01:00
// disable autocommit to allow correct behavior (default is true)
begin() {
return Promise.resolve();
2016-09-12 18:45:35 -04:00
}
2016-03-02 17:07:05 +01:00
commit(conn, value) {
this._completed = true;
return conn
.commitAsync()
2016-03-02 17:07:05 +01:00
.return(value)
.then(this._resolver, this._rejecter);
2016-09-12 18:45:35 -04:00
}
2016-03-02 17:07:05 +01:00
release(conn, value) {
return this._resolver(value);
2016-09-12 18:45:35 -04:00
}
2016-03-02 17:07:05 +01:00
rollback(conn, err) {
this._completed = true;
debugTx('%s: rolling back', this.txid);
return conn
.rollbackAsync()
2016-03-02 17:07:05 +01:00
.throw(err)
.catch((error) => {
if (isUndefined(error)) {
error = new Error(`Transaction rejected with non-error: ${error}`);
}
return this._rejecter(error);
});
2016-09-12 18:45:35 -04:00
}
2016-03-02 17:07:05 +01:00
acquireConnection(config) {
const t = this;
return Promise.try(() => config.connection || t.client.acquireConnection())
.then((connection) => {
connection.__knexTxId = this.txid;
return connection;
})
.tap((connection) => {
if (!t.outerTx) {
connection.setAutoCommit(false);
}
})
.disposer((connection) => {
debugTx('%s: releasing connection', t.txid);
connection.setAutoCommit(true);
if (!config.connection) {
t.client.releaseConnection(connection);
} else {
debugTx('%s: not releasing external connection', t.txid);
}
});
2016-03-02 17:07:05 +01:00
}
2016-09-12 18:45:35 -04:00
}