1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 | const assert = require('assert');
|
8 | const util = require('util');
|
9 | const EventEmitter = require('events').EventEmitter;
|
10 | const debug = require('debug')('loopback:connector:transaction');
|
11 | const uuid = require('uuid');
|
12 | const {createPromiseCallback} = require('./utils');
|
13 |
|
14 | module.exports = Transaction;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function Transaction(connector, connection) {
|
23 | this.connector = connector;
|
24 | this.connection = connection;
|
25 | EventEmitter.call(this);
|
26 | }
|
27 |
|
28 | util.inherits(Transaction, EventEmitter);
|
29 |
|
30 |
|
31 | Transaction.SERIALIZABLE = 'SERIALIZABLE';
|
32 | Transaction.REPEATABLE_READ = 'REPEATABLE READ';
|
33 | Transaction.READ_COMMITTED = 'READ COMMITTED';
|
34 | Transaction.READ_UNCOMMITTED = 'READ UNCOMMITTED';
|
35 |
|
36 | Transaction.hookTypes = {
|
37 | BEFORE_COMMIT: 'before commit',
|
38 | AFTER_COMMIT: 'after commit',
|
39 | BEFORE_ROLLBACK: 'before rollback',
|
40 | AFTER_ROLLBACK: 'after rollback',
|
41 | TIMEOUT: 'timeout',
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | Transaction.prototype.commit = function(cb) {
|
50 | cb = cb || createPromiseCallback();
|
51 | if (cb.promise) {
|
52 | this.connector.commit(this.connection, cb);
|
53 | return cb.promise;
|
54 | } else {
|
55 | return this.connector.commit(this.connection, cb);
|
56 | }
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | Transaction.prototype.rollback = function(cb) {
|
65 | cb = cb || createPromiseCallback();
|
66 | if (cb.promise) {
|
67 | this.connector.rollback(this.connection, cb);
|
68 | return cb.promise;
|
69 | } else {
|
70 | return this.connector.rollback(this.connection, cb);
|
71 | }
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | Transaction.begin = function(connector, options, cb) {
|
81 | if (typeof options === 'function' && cb === undefined) {
|
82 | cb = options;
|
83 | options = {};
|
84 | }
|
85 | cb = cb || createPromiseCallback();
|
86 | if (typeof options === 'string') {
|
87 | options = {isolationLevel: options};
|
88 | }
|
89 | const isolationLevel = options.isolationLevel || Transaction.READ_COMMITTED;
|
90 | assert(isolationLevel === Transaction.SERIALIZABLE ||
|
91 | isolationLevel === Transaction.REPEATABLE_READ ||
|
92 | isolationLevel === Transaction.READ_COMMITTED ||
|
93 | isolationLevel === Transaction.READ_UNCOMMITTED, 'Invalid isolationLevel');
|
94 |
|
95 | debug('Starting a transaction with options: %j', options);
|
96 | assert(typeof connector.beginTransaction === 'function',
|
97 | 'beginTransaction must be function implemented by the connector');
|
98 | connector.beginTransaction(isolationLevel, function(err, connection) {
|
99 | if (err) {
|
100 | return cb(err);
|
101 | }
|
102 | let tx = connection;
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | if (connection.connector == undefined || connection.connection == undefined ||
|
109 | connection.commit == undefined || connection.rollback == undefined) {
|
110 | tx = new Transaction(connector, connection);
|
111 | }
|
112 |
|
113 | tx.id = uuid.v1();
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | if (options.timeout) {
|
119 | tx.timeout = setTimeout(function() {
|
120 | const context = {
|
121 | transaction: tx,
|
122 | operation: 'timeout',
|
123 | };
|
124 | tx.notifyObserversOf('timeout', context, function(err) {
|
125 | if (!err) {
|
126 | tx.rollback(function() {
|
127 | debug('Transaction %s is rolled back due to timeout', tx.id);
|
128 | });
|
129 | }
|
130 | });
|
131 | }, options.timeout);
|
132 | }
|
133 | cb(err, tx);
|
134 | });
|
135 | if (cb.promise) return cb.promise;
|
136 | };
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | Transaction.prototype.isActive = function() {
|
142 | return !!this.connection;
|
143 | };
|