MongoDb在闲置约15分钟后断开连接

时间:2015-05-31 07:22:10

标签: javascript node.js mongodb

我有一个Node.js应用程序,它在应用程序启动时连接一次(到远程数据库),并将该连接变量用于所有操作。

MongoClient.connect(mongoConnStr, connOpts, function(err, db) {
    ...
}

connOpts是:

var connOpts = {
    server: { 
        socketOptions: {
            autoReconnect: true, 
            keepAlive: 1 
        }
    }
};

所以它连接得很好,但在闲置约15分钟后请求,它会挂起〜30秒然后抛出以下错误:

/home/me/Desktop/turbo/node_modules/mongodb/lib/utils.js:97
    process.nextTick(function() { throw err; });
                                        ^
MongoError: server myapp.cloudapp.net:27017 sockets closed
    at null.<anonymous> (/home/me/Desktop/turbo/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:301:47)
    at g (events.js:199:16)
    at emit (events.js:110:17)
    at null.<anonymous> (/home/me/Desktop/turbo/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:101:12)
    at g (events.js:199:16)
    at emit (events.js:110:17)
    at Socket.<anonymous> (/home/me/Desktop/turbo/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:139:12)
    at Socket.g (events.js:199:16)
    at Socket.emit (events.js:107:17)
    at TCP.close (net.js:485:12)

我知道定期ping数据库应该可以解决问题,但是我可能会忽略一些配置调整吗?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

run_until_complete(asyncio.async(blocking("ls"))

我找到了代码here