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

Commit 22783de

Browse files
committed
Merge pull request #460 from appirio-tech/sup-1481-java-bridge-reads
Sup 1481 java bridge reads
2 parents 71e0d96 + 4f186af commit 22783de

File tree

2 files changed

+89
-72
lines changed

2 files changed

+89
-72
lines changed

initializers/dataAccess.js

Lines changed: 75 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ var fs = require("fs");
3434
var async = require("async");
3535
var java = require('java');
3636
var Jdbc = require('informix-wrapper');
37+
var req = require('request');
3738
var helper;
3839

40+
var javaReadBridge = process.env.JAVA_READ_BRIDGE || "http://localhost:8082/bridge";
41+
3942
/**
4043
* Regex for sql paramters e.g @param_name@
4144
*/
@@ -110,6 +113,67 @@ function parameterizeQuery(query, params, callback) {
110113
});
111114
}
112115

116+
function executePreparedStatement(api, sql, parameters, connection, next, db) {
117+
async.waterfall([
118+
function (cb) {
119+
parameterizeQuery(sql, parameters, cb);
120+
},
121+
function (parametrizedQuery, cb) {
122+
sql = parametrizedQuery;
123+
124+
if (api.helper.readTransaction) {
125+
api.log("Calling Java Bridge", "debug");
126+
127+
api.log(sql, "debug");
128+
129+
var body = {
130+
"sql": new Buffer(sql).toString('base64'),
131+
"db": db
132+
};
133+
134+
api.log(JSON.stringify(body), "debug");
135+
136+
req({ url: javaReadBridge, method: "POST", body: body, json: true }, function(error, response, body) {
137+
if (error) {
138+
api.log(error, "error");
139+
cb(error);
140+
}
141+
142+
if (response.statusCode != 200) {
143+
api.log(response, "error");
144+
cb(response.statusMessage);
145+
}
146+
147+
api.log("Response:" + JSON.stringify(body), "debug");
148+
cb(null, body.results);
149+
});
150+
} else {
151+
api.log("Database connected", 'debug');
152+
// the connection might have been closed due to other errors, so this check must be done
153+
if (connection.isConnected()) {
154+
// Run the query
155+
connection.query(sql, cb, {
156+
start: function (q) {
157+
api.log('Start to execute ' + q, 'debug');
158+
},
159+
finish: function (f) {
160+
api.log('Finish executing ' + f, 'debug');
161+
}
162+
}).execute();
163+
} else cb("Connection closed unexpectedly");
164+
}
165+
}
166+
], function (err, result) {
167+
if (err) {
168+
api.log("Error occurred: " + err + " " + (err.stack || ''), 'error');
169+
} else {
170+
api.log("Query executed", "debug");
171+
}
172+
173+
next(err, result);
174+
});
175+
}
176+
113177

114178
/**
115179
* Expose the "dataAccess" utility.
@@ -239,9 +303,10 @@ exports.dataAccess = function (api, next) {
239303
return;
240304
}
241305

242-
connection = connectionMap[queries[queryName].db];
243-
244-
error = helper.checkObject(connection, "connection");
306+
if (!api.helper.readTransaction) {
307+
connection = connectionMap[queries[queryName].db];
308+
error = helper.checkObject(connection, "connection");
309+
}
245310

246311
if (error) {
247312
next(error);
@@ -254,36 +319,8 @@ exports.dataAccess = function (api, next) {
254319
next('The query for name ' + queryName + ' is not registered');
255320
return;
256321
}
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-
});
322+
323+
executePreparedStatement(api, sql, parameters, connection, next, queries[queryName].db);
287324
},
288325

289326
/**
@@ -316,45 +353,17 @@ exports.dataAccess = function (api, next) {
316353
return;
317354
}
318355

319-
connection = connectionMap[dbName];
320-
321-
error = helper.checkObject(connection, "connection");
356+
if (!api.helper.readTransaction) {
357+
connection = connectionMap[dbName];
358+
error = helper.checkObject(connection, "connection");
359+
}
322360

323361
if (error) {
324362
next(error);
325363
return;
326364
}
327365

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-
366+
executePreparedStatement(api, sql, parameters, connection, next, dbName);
358367
}
359368
};
360369
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)