Mongoose & Concurrent DB connections with read / write.

252 views
Skip to first unread message

Dimitrii Pokrovskii

unread,
Sep 19, 2013, 1:16:26 PM9/19/13
to mongoo...@googlegroups.com
hello everyone.

This seems very straight forward but I can't get it to work so if anyone has any ideas I would greatly appreciate it. 

I am running node & mongoDB local. I wrote a basic schema that capture timestamp & ip address. Everything works fine when I don't try to access the database more then once at a time. 

I wrote a basic shell script that pings node with requests to add entries to the database every 100 ms and ran 2 instances of the shell script. When both are running mongoose errors out every once and while on mongoose.connect. giving err = { state : 1 } or { state : 2 } 

Seems like something is preventing concurrent connections. Is there anything I can do on the mongoose.connect end to make this work better? or do i need to config mongoDB differently?  (currently it's latest stable build out of the npm without any config done)

Here is my connection code ( nodejs + express + mongoose ):



var connectDb = function (res, callback) {
 
  mongoose
.connect(mongourl);


 
var db = mongoose.connection;
 
  db
.on('error', function (err) {

    res
.json(500, err);
   
 
});


  db
.once('open', callback);
};


After this code is run the callback creates & adds a new document then closes the connection when done. I get no errors inside the DB when adding documents.

Any thoughts would be greatly appreciated.

Thanks.

Simon Holmes

unread,
Sep 20, 2013, 4:06:33 AM9/20/13
to mongoo...@googlegroups.com
Typically you should keep your Mongoose connection open while the Node server is running. See: http://theholmesoffice.com/mongoose-connection-best-practice/

This should solve the actual problem you're facing.

In addition you can specify a connection poolSize for a given connection: http://mongoosejs.com/docs/connections.html#connection_pools
Or you can create additional names connections: http://mongoosejs.com/docs/connections.html#multiple_connections
Reply all
Reply to author
Forward
0 new messages