-
Notifications
You must be signed in to change notification settings - Fork 4
/
transaction.js
339 lines (300 loc) · 8.74 KB
/
transaction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var inherits = require('inherits')
var FSM = require('yafsm')
var once = require('once')
module.exports = begin
begin.Transaction = Transaction
function begin (queryable, options, beginStatement, callback) {
if (typeof options != 'object') {
callback = beginStatement;
beginStatement = options;
options = {};
}
if (undefined === options.autoRollback) {
options.autoRollback = true;
}
if (typeof beginStatement == 'function') {
callback = beginStatement
beginStatement = undefined
}
if (queryable instanceof Transaction) {
return beginWithParent(queryable, options, callback)
}
var adapter = queryable.adapter;
var tx = new Transaction({
adapter: adapter,
begin: beginStatement,
callback: callback,
autoRollback: options.autoRollback
})
if (typeof adapter.createQuery != 'function' ||
typeof queryable.query != 'function') {
var error = new TypeError(queryable + ' is not a queryable!')
if (callback) {
callback(error)
} else {
throw error
}
}
if (typeof queryable.acquire == 'function') {
// it's a pool
var pool = queryable
pool.acquire(function (err, conn) {
if (err) return process.nextTick(function () {
tx.emit('error', err)
})
var release = pool.release.bind(pool, conn)
tx.on('query', pool.emit.bind(pool, 'query'))
tx.once('rollback:complete', release)
.once('commit:complete', release)
.setConnection(conn)
})
}
else {
// it's a connection
tx.setConnection(queryable)
}
return tx
}
inherits(Transaction, FSM)
function Transaction(opts) {
opts = opts || {}
this.adapter = opts.adapter
this._connection = null
this._statements = {
begin: opts.begin || 'BEGIN',
commit: opts.commit || 'COMMIT',
rollback: opts.rollback || 'ROLLBACK'
}
this._queue = []
this._nestingLevel = opts.nestingLevel || 0
this._autoRollback = opts.autoRollback;
this._emitQuery = function (query) { this.emit('query', query) }.bind(this)
this.handleError = this.handleError.bind(this)
FSM.call(this, 'disconnected', {
'disconnected': [ 'connected' ],
'connected': [ 'open', 'closed' ],
'open': [ 'connected', 'closed' ]
})
if (opts.callback) {
var callback = opts.callback
this
.once('error', callback)
.once('begin:complete', function () {
this.removeListener('error', callback)
callback(null, this)
})
}
}
Transaction.prototype.handleError = function (err, skipEmit) {
var self = this
var rollback = this.rollback.implementations['open']
if (this.state() !== 'closed' && this._connection && this._autoRollback) {
rollback.call(this, function (rollbackErr) {
if (rollbackErr) {
rollbackErr.previous = err;
self.emit('error', rollbackErr)
}
else if (!skipEmit) {
self.emit('error', err)
}
}, err)
}
else if (!skipEmit) self.emit('error', err)
}
Transaction.prototype.query = FSM.method('query', {
'connected|disconnected': function (text, params, callback) {
var query = this.adapter.createQuery(text, params, callback)
this._queue.push(query)
return query
},
'open': function (text, params, callback) {
var self = this
var query = this.adapter.createQuery(text, params, callback)
query.once('error', function (err) {
self.handleError(err, query.listeners('error').length)
})
return this._connection.query(query)
}
})
;['commit', 'rollback'].forEach(function (methodName) {
Transaction.prototype[methodName] = FSM.method(methodName, {
'open': closeVia(methodName),
'connected|disconnected': function (callback) {
var fn = this[methodName].implementations['open']
this._queue.push([fn, [callback]])
return this
}
})
})
Transaction.prototype.setConnection = FSM.method('setConnection', {
'disconnected': function (connection) {
var self = this
var err = self.state('connected')
if (err) {
process.nextTick(function () {
self.emit('error', err)
})
return
}
self._connection = connection
self._addConnectionListeners()
self.emit('begin:start')
var beginQuery = connection.query(self._statements.begin, function (err) {
if (err) return self.handleError(err)
self.emit('begin:complete') // removes error listener
self._runQueue()
})
self.emit('query', beginQuery)
return self
}
})
Transaction.prototype._runQueue = function () {
var self = this
return next()
function next (err, skipEmit) {
if (err) {
self.handleError(err, skipEmit)
}
if (!self._queue.length) {
if (self.state() !== 'closed' && (err = self.state('open'))) {
self.handleError(err)
}
return
}
var task = self._queue.shift()
if (Array.isArray(task)) {
runFunctionCall(self, task, next)
} else if (task instanceof Transaction) {
runChildTransaction(self, task)
} else {
runQueuedQuery(self, task, next)
}
}
}
function runFunctionCall (ctx, fnAndArgs, next) {
var fn = fnAndArgs[0]
, args = fnAndArgs[1]
, last = args[args.length - 1]
if (typeof last == 'function') {
args[args.length - 1] = function (err) {
if (err) return last(err)
last.apply(this, arguments)
next()
}
} else {
args.push(next)
}
return fn.apply(ctx, args)
}
function runQueuedQuery (self, query, next) {
if (self.state() == 'closed') {
self.query(query, function (err) {
query.emit('error', err)
next()
})
return
}
self._connection.query(query)
var onext = once(next) // ensure we only call `next` once
query.once('error', function (err) {
onext(err, this.listeners('error').length)
})
query.once('close', function () {
// let 'error' events have a chance to call `next` first
process.nextTick(onext)
})
}
function beginWithParent (parent, options, callback) {
var child = createChildTransaction(parent, options, callback)
switch (parent.state()) {
case 'disconnected':
case 'connected':
parent._queue.push(child)
break
case 'open':
runChildTransaction(parent, child)
break
case 'closed':
var error = new Error("Cannot start child transaction on parent in state 'closed'")
process.nextTick(function () {
// callback is already attached to error event
child.emit('error', error)
})
}
return child
}
function createChildTransaction (parent, options, callback) {
var nestingLevel = parent._nestingLevel + 1
var savepointName = 'sp_' + nestingLevel
var child = new Transaction({
adapter: parent.adapter,
nestingLevel: nestingLevel,
callback: callback,
begin: 'SAVEPOINT ' + savepointName,
commit: 'RELEASE SAVEPOINT ' + savepointName,
rollback: 'ROLLBACK TO ' + savepointName,
autoRollback: options.autoRollback,
})
child
.on('query', parent._emitQuery)
.once('connected', function () {
parent._removeConnectionListeners()
parent.state('connected')
})
.once('close', function () {
parent._addConnectionListeners()
parent._runQueue()
})
return child
}
function runChildTransaction (parent, child) {
// Child transaction
child.setConnection(parent._connection)
child.on('error', function (err) {
if (child.listeners('error').length == 1) {
// if a child transaction errors, and the parent is the only
// listener, it should re-emit the error, but *not* roll back
parent.emit('error', err)
}
})
}
Transaction.prototype._addConnectionListeners = function () {
this._connection.on('error', this.handleError)
this._connection.on('query', this._emitQuery)
}
Transaction.prototype._removeConnectionListeners = function () {
this._connection.removeListener('error', this.handleError)
this._connection.removeListener('query', this._emitQuery)
}
function closeVia (action) {
return function (callback) {
var self = this
if (self.state() == 'closed') {
return
}
var err = self.state('closed')
if (err) {
return self.handleError(err, callback)
}
self.emit(action + ':start')
var q = self._connection.query(self._statements[action], function (err) {
self._removeConnectionListeners()
self._connection = null
if (err) {
self.handleError(new CloseFailedError(err, action), callback)
} else {
self.emit(action + ':complete')
self.emit('close')
if (callback) callback()
}
})
return self
}
}
inherits(CloseFailedError, Error)
function CloseFailedError(err, action) {
Error.captureStackTrace(this, CloseFailedError)
this.name = action + ' failed'
this.message = err.message
this.previous = null;
}