javascript - Problems encountered by mongoose in Baidu bae long connection
滿天的星座
滿天的星座 2017-05-31 10:36:10
0
1
1062

Baidu bae mongodb does not support long connections, so a reconnection mechanism is required. I saw others on the Internet using mongoose.createConnection(), but when I used createConnection(), I would get an error and couldn’t connect at all
But mongoose.connect() could connect, so I wrote a mongoose.connect() reuse Connection version
`var db = mongoose.connection;
db.on('error',function(error){

mongoose.disconnect();

})
db.on('disconnected', function() {

mongoose.connect(config.mongo, {server:{auto_reconnect:true,poolSize:10}});

});
mongoose.connect(config.mongo, {server:{auto_reconnect:true,poolSize:10}});`
The configh.mongo inside is the url to connect to the database!
Please tell me how to solve this problem

滿天的星座
滿天的星座

reply all(1)
洪涛

1. Question: But when I use createConnection(), I get an error and cannot connect at all.

var conn = mongoose.createConnection('your connection string'),
    MyModel = conn.model('ModelName', schema),
    m = new MyModel;
    m.save(); // works

When using createConnection, mongoose.model cannot be used, please refer to the chestnut above.

2. Baidu bae supports 60 minutes of connection, which should be enough.

Quoted official website information: After the public MongoDB connection is established, if it is idle for 1 hour without any activity, the connection will be disconnected by the server. If a new connection request is initiated when the number of concurrent connections has reached the upper limit of the quota, the server will search the existing connections to see if there are any that have been idle for more than 30 seconds, and if so, disconnect the least recently used (Least Recently Used) connections. ) and accept new connection requests; otherwise, reject new connection requests. Please pay attention to handling the problem that MongoDB connections that have been idle for a long time will be disconnected in the application code.

For reference.

Love MongoDB! Have fun!


The 2017 MongoDB Chinese Community Beijing User Group Conference is coming soon, June 3, 2017 13:00-18:00

Hurry and sign up to participate! Click Me

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template