Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose output the error "Error: connection closed"

I stumble upon a curious problem about mongoose connect the mongodb, it generate the detail errors as the following

    e:\Mentor_Resources\node\node_twitter_bootstrap>node app
Express server listening on port 3000
Trace: error occure when start to connect dbError: connection closed
    at e:\Mentor_Resources\node\node_twitter_bootstrap\server\module\word.js:14:
17
    at Connection.open (e:\Mentor_Resources\node\node_twitter_bootstrap\node_mod
ules\mongoose\lib\connection.js:201:5)
    at Db.open (e:\Mentor_Resources\node\node_twitter_bootstrap\node_modules\mon
goose\node_modules\mongodb\lib\mongodb\db.js:247:16)
    at Server.connect.connectionPool.on.server._serverState (e:\Mentor_Resources
\node\node_twitter_bootstrap\node_modules\mongoose\node_modules\mongodb\lib\mong
odb\connection\server.js:413:7)
    at EventEmitter.emit (events.js:115:20)
    at connection.on.connectionStatus (e:\Mentor_Resources\node\node_twitter_boo
tstrap\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connect
ion_pool.js:108:15)
    at EventEmitter.emit (events.js:91:17)
    at Socket.closeHandler (e:\Mentor_Resources\node\node_twitter_bootstrap\node
_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:401:
12)
    at Socket.EventEmitter.emit (events.js:88:17)
    at Socket._destroy.destroyed (net.js:364:10)

the code snippet of mongoose is:

var mongoose = require('mongoose');

mongoose.connect("mongodb://localhost/word-sentence",function(err) {
    if(err)
        console.trace('error occure when start to connect db' + err);
});

i am sure the mongodb is open, and i restart mongodb for several times,but the error is still exist, so I reboot my Windows XP , and try again the problem disappear,everything is ok, so I want to know why?

like image 754
clevertension Avatar asked Dec 05 '25 10:12

clevertension


1 Answers

This is a common problem when pooled connections in longer running applications return connection closed.

The mongoose documentation recommends adding keepAlive to the options object you pass into the connect function.

Here's an example (you can remove replset if you're not using this),

// include keep alive for closing connections,
// http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
var mongoOptions =
{
    db: {safe: true},
    server: {
        socketOptions: {
            keepAlive: 1
        }
    },
    replset: {
        rs_name: 'myReplSet',
        socketOptions: {
            keepAlive: 1
        }
    }
};

mongoose.connect( YOUR_URI, mongoOptions );

mongoose.connection.on('error', function(err) {
    console.log('Mongo Error:\n');
    console.log(err);
}).on('open', function() {
    console.log('Connection opened');
});
like image 90
Eat at Joes Avatar answered Dec 07 '25 22:12

Eat at Joes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!