Skip to content

Commit

Permalink
get connection from pool for each query in 'N queries' test to allow …
Browse files Browse the repository at this point in the history
…them run in parallel
  • Loading branch information
sidorares committed Apr 10, 2013
1 parent 478b0ca commit eaa7f0e
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions nodejs/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function mongodbDriverQuery(callback) {
process.nextTick(function() {
collection.find({ id: getRandomNumber()}).toArray(function(err, world) {
callback(err, world[0]);
});
});
})
}

Expand Down Expand Up @@ -112,7 +112,7 @@ http.createServer(function (req, res) {

async.parallel(queryFunctions, function(err, results) {
res.end(JSON.stringify(results));
});
});
break;

case '/mongoose':
Expand Down Expand Up @@ -150,30 +150,30 @@ http.createServer(function (req, res) {

case '/mysql':
res.writeHead(200, {'Content-Type': 'application/json'});

pool.getConnection(function(err, connection) {
if (err || !connection) {
return res.end('MYSQL CONNECTION ERROR.');
}

function mysqlQuery(callback) {
connection.query("SELECT * FROM World WHERE id = " + getRandomNumber(), function(err, rows) {
function mysqlQuery(callback) {
pool.getConnection(function(err, connection) {
if (err) callback(err);
connection.query("SELECT * FROM foos WHERE id = " + getRandomNumber(), function(err, rows) {
callback(null, rows[0]);
connection.end();
});
}
});
}

var values = url.parse(req.url, true);
var queries = values.query.queries || 1;
var queryFunctions = new Array(queries);
var values = url.parse(req.url, true);
var queries = values.query.queries || 1;
var queryFunctions = new Array(queries);

for (var i = 0; i < queries; i += 1) {
queryFunctions[i] = mysqlQuery;
for (var i = 0; i < queries; i += 1) {
queryFunctions[i] = mysqlQuery;
}
async.parallel(queryFunctions, function(err, results) {
if (err) {
res.writeHead(500);
return res.end('MYSQL CONNECTION ERROR.');
}

async.parallel(queryFunctions, function(err, results) {
res.end(JSON.stringify(results));
connection.end();
});
res.end(JSON.stringify(results));
});
break;

Expand Down

0 comments on commit eaa7f0e

Please sign in to comment.