[英]Node.js Mongodb-native driver connection sharing
main.js main.js
var http = require('http');
var UserModel = require('./models/user.js');
var server = http.createServer(function(req, res){
UserModel.create({
}), function(e, o){
if(e) { console.log(e); } else {
} console.log(o); }
});
}).listen(3000);
connections.js connections.js
var mongo = require('mongodb');
module.exports = {
dbMain: new mongo.Db('main', new mongo.Server('127.0.0.1', 27017, { auto_reconnect: true }, {})),
dbLog: new mongo.Db('log', new mongo.Server('127.0.0.1', 27017, { auto_reconnect: true }, {}))
};
/models/user.js /models/user.js
var mongodb = require('mongodb');
var db = require('./connections.js').dbMain;
module.exports = {
create: function(newData, callback){
db.open(function(e, db){
db.collection('users', function(e, collection){
collection.insert(newData, callback);
});
});
}
}
When I use the code above, the server crashes with the problem that, the SECOND time a request comes in, we still have the database connection opened, so lets add db.close to our Users.create function. 当我使用上面的代码时,服务器崩溃的问题是,在请求进入的第二次,我们仍然打开了数据库连接,所以我们将db.close添加到我们的Users.create函数中。
create: function(newData, callback){
db.open(function(e, db){
db.collection('users', function(e, collection){
collection.insert(newData, function(e, o){
db.close(); // Voila.
callback(e, o);
});
});
});
}
At this stage the server CAN still crash, because of multiple connections open, I don't understand why or how this can happen but it does. 在这个阶段,服务器CAN仍然崩溃,因为打开了多个连接,我不明白为什么或如何发生这种情况,但确实如此。
How do I organize my project into models (I don't want to use Mongoose, my validation is done in a different layer not the model, so Mongoose would be an overkill for me)? 如何将我的项目组织到模型中(我不想使用Mongoose,我的验证是在不同的模型中进行的,所以Mongoose对我来说会有点过分)? Also how do I handle connections in the project?
另外我如何处理项目中的连接?
you could have a library that wraps all this up nicely - it means that only one connection to the database will be opened and the same (open) connection will be returned for the second request - if you are getting 1000+ per second, this is a make-or-break issue (ie not re-opening the connection each request)... 你可以有一个很好地包装所有这一切的库 - 这意味着只打开一个与数据库的连接,并为第二个请求返回相同的(开放)连接 - 如果你得到每秒1000+,这是一个成败问题(即不重新打开每个请求的连接)......
users.js : users.js :
var connections = require('./connections.js');
var serverCache = connections('127.0.0.1', 27017);
module.exports = {
create: function(newData, callback){
serverCache('main', 'users', function(e, collection){
collection.insert(newData, callback);
})
}
}
connections.js connections.js
var mongo = require('mongodb');
// a mongo connection cache
// pass in host & port
// it returns a function accepting dbName, collectionName & callback
var mongoCache = function(host, port){
// keep our open connections
var mongoDatabases = {};
var ensureDatabase = function(dbName, readyCallback){
// check if we already have this db connection open
if(mongoDatabases[dbName]){
readyCallback(null, mongoDatabases[dbName]);
return;
}
// get the connection
var server = new mongo.Server(host, port, {auto_reconnect: true});
// get a handle on the database
var db = new mongo.Db(dbName, server);
db.open(function(error, databaseConnection){
if(error) throw error;
// add the database to the cache
mongoDatabases[dbName] = databaseConnection;
// remove the database from the cache if it closes
databaseConnection.on('close', function(){
delete(mongoDatabases[dbName]);
})
// return the database connection
readyCallback(error, databaseConnection);
})
}
var ensureCollection = function(dbName, collectionName, readyCallback){
ensureDatabase(dbName, function(error, databaseConnection){
if(error) throw error;
databaseConnection.createCollection(collectionName, function(error, collection) {
if(error) throw error;
// return the collection finally
readyCallback(error, collection);
})
})
}
return ensureCollection;
}
module.exports = mongoCache;
I'm currently using a global connection with multiple http requests. 我目前正在使用具有多个http请求的全局连接。 In the past I created a complex library that was creating several connections to MongoDB and picking up one randomly for each connection.
在过去,我创建了一个复杂的库,它创建了几个与MongoDB的连接,并为每个连接随机选取一个。
Later I found that the native driver can do that for me, which is pretty neat. 后来我发现本机驱动程序可以为我做这个,这很漂亮。 Currently I'm using a single object, and the driver chooses to which connection send the query.
目前我正在使用单个对象,驱动程序选择向哪个连接发送查询。
var srvOpts = {
auto_reconnect: true,
poolSize: 10,
};
var conn = new Mongo.Server("localhost", 27017, srvOpts),
db = new Mongo.Db("dbname", conn, {});
db.open(function (){});
As you can this is a great idea, I'm thinking to copy that idea into the Redis driver that I'm using, but I'm short on time so I doubt that I will do any time soon. 你可以这是一个好主意,我想把这个想法复制到我正在使用的Redis驱动程序中,但我的时间很短,所以我怀疑我会很快做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.