我的应用程序基于 asp.net (MVC) 和 mongodb。
我正在跟踪每个客户端请求并将其写入我的 mongodb 数据库。
目前,每个客户端请求都是打开新的数据库连接。
这是我的代码:
public class MyController
{
public ActionResult MyOperation(string input)
{
const string connectionString = "mongodb://URL";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("dbname");
var collection = database.GetCollection<ElcoRequest>("MyCollection");
// Create new row
}
}
我想对所有请求使用相同的数据库连接。 (而不是我当前为每个数据库打开新连接的解决方案)
我知道在使用 ADO.NET 时,有一种叫做“连接池”的东西。 mongodb 中的等价物是什么?
最佳答案
连接池在 MongoClient
类中进行内部处理。您不需要自己实现池化。
只要您使用相同的连接字符串和设置,您就可以像现在一样创建新的 MongoClient
实例。
MongoDB MongoClient
documentation :
The connections to the server are handled automatically behind the scenes (a connection pool is used to increase efficiency).
关于c# - MVC中的mongodb连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24819569/