transaction.js
948 Bytes
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
'use strict';
/**
* Module dependencies.
*/
const util = require('util');
const wrap = require('co-wrap-all');
const Operator = require('./operator');
module.exports = RDSTransaction;
function RDSTransaction(conn) {
Operator.call(this);
this.conn = conn;
this.isCommit = false;
this.isRollback = false;
}
util.inherits(RDSTransaction, Operator);
const proto = RDSTransaction.prototype;
proto.commit = function* () {
this._check();
try {
return yield this.conn.commit();
} finally {
this.conn.release();
this.conn = null;
}
};
proto.rollback = function* () {
this._check();
try {
return yield this.conn.rollback();
} finally {
this.conn.release();
this.conn = null;
}
};
proto._query = function* (sql) {
this._check();
return yield this.conn._query(sql);
};
proto._check = function() {
if (!this.conn) {
throw new Error('transaction was commit or rollback');
}
};
wrap(proto);