Hi, I have a backend published and working in BlueMix.
Here is its URL: http://crmbackend.ng.bluemix.net/customer/list
After calling this URL a few times, the service fails, and It stops working.
If I go to the log, into BlueMix it doesn't work.
Here is the code for request above:
//Lists all customers
exports.list_customer = function (req, res) { db.connect(mongo.url, function (err, conn) { var collection = conn.collection('customers');
// list messages
collection.find().toArray(function (err2, items) {
res.send(items);
});
});
Thanks in advance.
Answer by SteveKinder (566) | Mar 28, 2014 at 11:34 AM
Perhaps you are running out of connections? The community Mongo service has a connection limit of 100 connections... You might try closing or ending your connection and see if your app becomes more reliable?
Thanks for your help. Full solution has been attached to this thread
Answer by Andrew Huffman (343) | Mar 28, 2014 at 11:48 AM
Without an indication as to what the error was it'd be hard to determine the issue. Can you add a log statement in your callback function?
//Lists all customers
exports.list_customer = function (req, res) {
db.connect(mongo.url, function (err, conn) {
if(err) { console.log("Error connecting: "+err) }
else {
var collection = conn.collection('customers');
// list messages
collection.find().toArray(function (err, items) {
if (err) {
console.log("Error on find: "+ err);
} else {
res.send(items);
}
});
});
};
Thanks for your help. Full solution has been attached to this thread
Answer by DGualda (65) | Mar 28, 2014 at 01:09 PM
Hi guys, It seems the reason was the 100 mongodb connections.
To simulate this fact in local I started my local mongodb like: mongod.exe --maxConns 100
The solution for release connections is:
//Lists all customers
exports.list_customer = function (req, res) { db.connect(mongo.url, function (err, conn) { var collection = conn.collection('customers');
// list messages
collection.find().toArray(function (err, items) {
if (err) {
console.log("Error on find: " + err);
} else {
res.send(items);
conn.close();
}
}
);
});
};
Thank you very much for your help.
DGualda, you may want to just keep the connection open for the life-time of the server. Presumably you'll be using it often, so opening and closing it all the time is just extra work.
Deploying an app using MongoLabs instead of MongoDB Service 3 Answers
I get a service broker error when I try to delete an app 7 Answers
Node.js BlueMix Applikation - Post Request 10 Answers
I am unable to push my Node.js project with mongodb backend on Bluemix 2 Answers
MongoDB problem on Bluemix/NodeJS. Application not start. 2 Answers