Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 7ebf693

Browse files
committed
setup to skip DB on read transactions
1 parent 71e0d96 commit 7ebf693

File tree

2 files changed

+63
-72
lines changed

2 files changed

+63
-72
lines changed

initializers/dataAccess.js

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,44 @@ function parameterizeQuery(query, params, callback) {
110110
});
111111
}
112112

113+
function executePreparedStatement(api, sql, parameters, connection, next) {
114+
async.waterfall([
115+
function (cb) {
116+
parameterizeQuery(sql, parameters, cb);
117+
}, function (parametrizedQuery, cb) {
118+
sql = parametrizedQuery;
119+
120+
if (api.helper.readTransaction) {
121+
api.log("CALLING SANTTOSH'S MAGIC");
122+
api.log(new Buffer(sql).toString('base64'));
123+
cb(null, []); // necessary?
124+
} else {
125+
api.log("Database connected", 'debug');
126+
// the connection might have been closed due to other errors, so this check must be done
127+
if (connection.isConnected()) {
128+
// Run the query
129+
connection.query(sql, cb, {
130+
start: function (q) {
131+
api.log('Start to execute ' + q, 'debug');
132+
},
133+
finish: function (f) {
134+
api.log('Finish executing ' + f, 'debug');
135+
}
136+
}).execute();
137+
} else cb("Connection closed unexpectedly");
138+
}
139+
}
140+
], function (err, result) {
141+
if (err) {
142+
api.log("Error occurred: " + err + " " + (err.stack || ''), 'error');
143+
} else {
144+
api.log("Query executed", "debug");
145+
}
146+
147+
next(err, result);
148+
});
149+
}
150+
113151

114152
/**
115153
* Expose the "dataAccess" utility.
@@ -239,9 +277,10 @@ exports.dataAccess = function (api, next) {
239277
return;
240278
}
241279

242-
connection = connectionMap[queries[queryName].db];
243-
244-
error = helper.checkObject(connection, "connection");
280+
if (!api.helper.readTransaction) {
281+
connection = connectionMap[queries[queryName].db];
282+
error = helper.checkObject(connection, "connection");
283+
}
245284

246285
if (error) {
247286
next(error);
@@ -254,36 +293,8 @@ exports.dataAccess = function (api, next) {
254293
next('The query for name ' + queryName + ' is not registered');
255294
return;
256295
}
257-
258-
async.waterfall([
259-
function (cb) {
260-
parameterizeQuery(sql, parameters, cb);
261-
}, function (parametrizedQuery, cb) {
262-
sql = parametrizedQuery;
263-
api.log("Database connected", 'debug');
264-
265-
// the connection might have been closed due to other errors, so this check must be done
266-
if (connection.isConnected()) {
267-
// Run the query
268-
connection.query(sql, cb, {
269-
start: function (q) {
270-
api.log('Start to execute ' + q, 'debug');
271-
},
272-
finish: function (f) {
273-
api.log('Finish executing ' + f, 'debug');
274-
}
275-
}).execute();
276-
} else cb("Connection closed unexpectedly");
277-
}
278-
], function (err, result) {
279-
if (err) {
280-
api.log("Error occurred: " + err + " " + (err.stack || ''), 'error');
281-
} else {
282-
api.log("Query executed", "debug");
283-
}
284-
285-
next(err, result);
286-
});
296+
297+
executePreparedStatement(api, sql, parameters, connection, next);
287298
},
288299

289300
/**
@@ -316,45 +327,17 @@ exports.dataAccess = function (api, next) {
316327
return;
317328
}
318329

319-
connection = connectionMap[dbName];
320-
321-
error = helper.checkObject(connection, "connection");
330+
if (!api.helper.readTransaction) {
331+
connection = connectionMap[dbName];
332+
error = helper.checkObject(connection, "connection");
333+
}
322334

323335
if (error) {
324336
next(error);
325337
return;
326338
}
327339

328-
async.waterfall([
329-
function (cb) {
330-
parameterizeQuery(sql, parameters, cb);
331-
}, function (parametrizedQuery, cb) {
332-
sql = parametrizedQuery;
333-
api.log("Database connected", 'info');
334-
335-
// the connection might have been closed due to other errors, so this check must be done
336-
if (connection.isConnected()) {
337-
// Run the query
338-
connection.query(sql, cb, {
339-
start: function (q) {
340-
api.log('Start to execute ' + q, 'debug');
341-
},
342-
finish: function (f) {
343-
api.log('Finish executing ' + f, 'debug');
344-
}
345-
}).execute();
346-
} else cb("Connection closed unexpectedly");
347-
}
348-
], function (err, result) {
349-
if (err) {
350-
api.log("Error occurred: " + err + " " + (err.stack || ''), 'error');
351-
} else {
352-
api.log("Query executed", "debug");
353-
}
354-
355-
next(err, result);
356-
});
357-
340+
executePreparedStatement(api, sql, parameters, connection, next);
358341
}
359342
};
360343
next();

initializers/transaction.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ exports.transaction = function (api, next) {
5959
* @param {Function} next - The callback function
6060
*/
6161
transactionPreProcessor = function (connection, actionTemplate, next) {
62-
if (actionTemplate.transaction === "read" || actionTemplate.transaction === "write") {
63-
var dbConnectionMap = {}, dbConnection, callback, connectionOpenedCount = 0;
62+
var dbConnectionMap = {}, dbConnection, callback, connectionOpenedCount = 0;
63+
64+
if (actionTemplate.transaction === "write") {
65+
api.helper.readTransaction = false;
6466

6567
var connectTimeout = function() {
6668
api.log("Timed out without obtaining all DB connections", "error");
@@ -120,6 +122,8 @@ exports.transaction = function (api, next) {
120122
});
121123

122124
} else {
125+
connection.dbConnectionMap = dbConnectionMap;
126+
api.helper.readTransaction = true;
123127
next(connection, true);
124128
}
125129
};
@@ -136,16 +140,20 @@ exports.transaction = function (api, next) {
136140
* @param {Function} next - The callback function
137141
*/
138142
transactionPostProcessor = function (connection, actionTemplate, toRender, next) {
139-
140143
var disconnectTimeout = function() {
141144
api.error("Timed out without closing all DB connections", "error");
142145
// I dont want to call next(connection); here because I want to allow the execution to to continue in case connection can be closed after timeout
143146
}
144147

145-
var clearMe = setTimeout(disconnectTimeout, DISCONN_TIMEOUT);
146-
147148
var connectionClosedCount = 0;
148-
if (connection.dbConnectionMap !== null && connection.dbConnectionMap !== undefined && actionTemplate.transaction !== null && actionTemplate.transaction !== undefined) {
149+
if (connection.dbConnectionMap !== null
150+
&& connection.dbConnectionMap !== undefined
151+
&& actionTemplate.transaction !== null
152+
&& actionTemplate.transaction !== undefined
153+
&& actionTemplate.transaction === "write") {
154+
155+
var clearMe = setTimeout(disconnectTimeout, DISCONN_TIMEOUT);
156+
149157
actionTemplate.databases.forEach(function (databaseName) {
150158
var callback;
151159
callback = function (err, result) {

0 commit comments

Comments
 (0)