我有一个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数据库应该可以解决问题,但是我可能会忽略一些配置调整吗?