2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
|
import Promise from '../../promise';
|
|
|
|
|
import Transaction from '../../transaction';
|
2016-05-18 19:59:24 +10:00
|
|
|
const debugTx = require('debug')('knex:tx')
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
|
function Oracle_Transaction(client, container, config, outerTx) {
|
|
|
|
|
Transaction.call(this, client, container, config, outerTx)
|
|
|
|
|
}
|
|
|
|
|
inherits(Oracle_Transaction, Transaction)
|
|
|
|
|
|
|
|
|
|
assign(Oracle_Transaction.prototype, {
|
|
|
|
|
|
|
|
|
|
// disable autocommit to allow correct behavior (default is true)
|
2016-05-17 01:01:34 +10:00
|
|
|
begin() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return Promise.resolve()
|
|
|
|
|
},
|
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
commit(conn, value) {
|
2016-03-02 17:07:05 +01:00
|
|
|
this._completed = true
|
|
|
|
|
return conn.commitAsync()
|
|
|
|
|
.return(value)
|
|
|
|
|
.then(this._resolver, this._rejecter)
|
|
|
|
|
},
|
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
release(conn, value) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return this._resolver(value)
|
|
|
|
|
},
|
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
rollback(conn, err) {
|
2016-03-02 17:07:05 +01:00
|
|
|
this._completed = true
|
|
|
|
|
debugTx('%s: rolling back', this.txid)
|
|
|
|
|
return conn.rollbackAsync()
|
|
|
|
|
.throw(err)
|
|
|
|
|
.catch(this._rejecter)
|
|
|
|
|
},
|
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
acquireConnection(config) {
|
|
|
|
|
const t = this
|
|
|
|
|
return Promise.try(() =>
|
|
|
|
|
config.connection || t.client.acquireConnection()
|
|
|
|
|
).tap(connection => {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (!t.outerTx) {
|
|
|
|
|
connection.setAutoCommit(false)
|
|
|
|
|
}
|
2016-05-17 01:01:34 +10:00
|
|
|
}).disposer(connection => {
|
2016-03-02 17:07:05 +01:00
|
|
|
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-05-17 01:01:34 +10:00
|
|
|
export default Oracle_Transaction
|