Closing mongodb connections - node.js

I use native mongodb driver with Expressjs, I don't want to open and close connections on a regular basis in my routes. I want to open once use one connection in all my next() functions and then close when done.
I saw that after opening a connection I can pass the db object to next() functions in request object and use it.
When I try to close the connection like var db=req.db; db.close(); it throws an error.
Inorder to solve this issue I have decided to use settimeout function to close my connection.
I can pass around and use the db obj and send response and the after 1-2 seconds db obj is closed by settimeout.
I'm worried if the requests are more will the settimeout functions effect the servers performance. If I use this trick to manage my db connections.

Sorry, I forgot to do:
db=client.db('test')
and:
db.close();
Change in later versions can't directly open db without getting the client, using native mongodb-nodejs driver.

Related

MongoDB connections on require or by function

I am working on a single pager that writes to different mongodb databases through an API setup with express. To do this I have one file named db.js that is doing all of the work with the mongoose module and then exporting the two connections to my express file called app.js.
When I start running my app file with node, my mongo console shows the two connections being made.
My question is, should I be making the exports structured so that they are functions that only connect to the DB when the functions themselves are called? Is there anything bad about leaving the two connections open and waiting for people to use them?
It is better to have your database connections created on start of the application, and leaving them open. The other way to create connections when an API call is made is extremely inefficient, because the connection load increases wrt number of API calls, and also because the response time of the API increases.
In db.js export your objects.
In your app.js, you can directly require the appropriate connection and start using it
var db = require("../db").first;
db.find({}, function (err, res) {})

Connect only once to Mongo

UPDATE: While building an example, I found that the culprit seems to be restify-enroute. The package is messing with mongoose connection. I published the example to https://github.com/HeavyStorm/mongoose-enroute-conflict.
I had understood that mongoose.connect was how to establish a connection between mongoose and Mongo, and supposed that this was a singleton connection of sorts - once called, every module in my app would be able to call Mongo.
This is probably wrong.
My current scenario:
I'm calling connect:
mongoose.connect("mongodb://localhost/test");
My apps structure:
/
/app.js
/modules
./users
./module.js
In app.js I call mongoose.connect. I also load and expose a route from module.js. When I receive a http call, module.js code kicks in (I can see that from the debugger), but as soon as I call mongo (through Model.find() in this case), well, code skips, my callbacks are never called in the client is held on a waiting state.
However, if I add the mongoose.connect line to module.js, the Model.find() yields to the callback almost instantly and the client's receives the response.
TLDR: Do I have to call mongoose.connect on every module that access the database? Why is that?

When should I call done() in node-postgres?

pg 4.4.3
I'm using socket.io to connect client-side to server. I've guessed, I suppose to connect server to database on server start, but there are a lot of warnings in pg "docs": 'use done() or bad things will happen'.
When should i use it? If i'm opening connection to the db and then creating socket.io server within it, and then using done() after each query, then i'm receiving error after 30 sec idle:
Error: This socket has been ended by the other party
May be I should create socket.io and then open connection to the db within each user session? or open connection to db on each query if it's not currently opened? Being honest, I don't get, why should i do this, why can't I just create single connection to the database on server start and send all queries through it instead of this open-close repetition.

Difference between using getConnection() and using pool directly in node.js with node-mysql module?

The documentation states that you can either use the pool directly with:
pool.query();
or get a connection manually and then run a query:
pool.getConnection(function(err, connection) {
// Use the connection
connection.query( 'SELECT something FROM sometable', function(err, rows) {
// And done with the connection.
connection.release();
// Don't use the connection here, it has been returned to the pool.
});
});
The second option is a lot of code that has to be repeated every time you need to run a query. Is it safe to just use the pool directly? Does pool.query() release the connection back into the pool when it's done?
Question kindly answered by developer on github:
https://github.com/felixge/node-mysql/issues/857#issuecomment-47382419
Is it safe to just use the pool directly?
Yes, as long as you are doing single statement queries. The only
reason you couldn't use that in your example above is because you are
making multiple queries that need to be done in sequential order on
the same connection. Calling pool.query() may be different connections
each time, so things like FOUND_ROWS() will not work as you intended
if the connection is not the same as the one that did the
SQL_CALC_FOUND_ROWS query. Using the long method allows you to hold
onto the same connection for all your queries.
Does pool.query() release the connection back into the pool when it's
done?
Yes

node.js mongodb closing the connection

I am trying to use node.js with mongodb and following the tutorial at http://howtonode.org/express-mongodb
The code for opening the connection is
ArticleProvider = function(host, port) {
this.db= new Db('node-mongo-blog', new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function(){});
};
However i cannot see any connections being closed.
But when i see the logs on the mongo console, i can see that are connections which open and they close after some time.
Does the connection close automatically? Will it be a problem when a large no of clients try to access the server? Where should the connection be closed?
Thanks
Tuco
In that example application, only a single ArticleProvider object is created for the application to share when serving requests. That object's constructor opens a db connection that won't be closed until the application terminates (which is fine).
So what you should see is that you get a new mongo connection each time you start your app, but no additional connections made no matter how many clients access the server. And shortly after you terminate your app you should see its connection disappear on the mongo side.
node-mongodb-native provides a close method for Db objects and you can close your connection when you are finished by calling it.
var that = this;
this.db.open(function(){
// do db work here
// close the connection
that.db.close();
});
If you don't close your connection, event loop keeps the connection open and your process doesn't exit. If you are building a web server where your process will not be terminated, it's not necessary for you to close the connection.
A better reference for node-mongodb-native can be found on https://github.com/mongodb/node-mongodb-native.
Remember to put the db.close in the last callback that gets executed so the connection is open until all callbacks are finished. Otherwise, it gives an error like
/usr/local/lib/node_modules/mongodb/lib/utils.js:97
process.nextTick(function() { throw err; });
^
Error
at Error.MongoError (/usr/local/lib/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
at Server.destroy (/usr/local/lib/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:629:47)
at Server.close (/usr/local/lib/node_modules/mongodb/lib/server.js:344:17)
at Db.close (/usr/local/lib/node_modules/mongodb/lib/db.js:267:19)

Resources