我应该什么时候打开和关闭 MongoDB 连接?

发布于 2025-01-03 07:46:57 字数 844 浏览 1 评论 0原文

一般来说,我对 MongoDB 和 NoSQL 非常陌生,我刚刚开始使用 MongoDB / Norm / ASP.NET MVC 3 构建一个网站。

我想知道应该如何确定与 Mongo 数据库的连接范围。

现在我有一个实例化 MongoSession 的 Basecontroller 和 onActionExecuted 我将其处置,以便我所有的派生控制器都可以访问我的 MongoSession。 MongoSession 类在其构造函数中打开一个连接,并通过 Dispose() 处理它,这就是它现在的工作方式。

private IMongo _mongo;

public MongoSession()
{         
    _mongo = Mongo.Create("connString");      
} 

public void Dispose()
{
    _mongo.Dispose();
}

我有点担心如果我还在控制器中做其他事情,它可能会保持连接打开太长时间。

这种方法是否足以避免冒保持太多连接打开的风险,或者我应该做一些更像下面的示例方法的事情?

   public void Add<T>(T item) where T : class, new()
   {
       using (var mongo = Mongo.Create("connString"))
       {
         mongo.GetCollection<T>().Insert(item); 
       }
   }

另一个后续问题是:

通过 Norm 操作打开和关闭 MongoDB 连接是否“昂贵”?

i am very new to MongoDB and NoSQL in general and i've just started building a site with MongoDB / Norm / ASP.NET MVC 3.

I am wondering how i should be scoping the connections to my Mongo database.

Right now i have a Basecontroller that instanciates the MongoSession and onActionExecuted i dispose it so all my deriving controllers will have access to my MongoSession. The MongoSession class opens a connection in its constructor and disposes it on Dispose(), the way it's working today.

private IMongo _mongo;

public MongoSession()
{         
    _mongo = Mongo.Create("connString");      
} 

public void Dispose()
{
    _mongo.Dispose();
}

I am a bit worried it might be holding connections open too long if i am doing other stuff aswell in the controllers.

Is that approach enought to not risking holding too many connections open or should i be doing something more like the example method below?

   public void Add<T>(T item) where T : class, new()
   {
       using (var mongo = Mongo.Create("connString"))
       {
         mongo.GetCollection<T>().Insert(item); 
       }
   }

Another follow up question is:

Are opening and closing MongoDB connections through Norm "expensive" operations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2025-01-10 07:46:57

我会让连接保持打开状态,因为重新创建连接的成本很高。 Mongo 很好,连接很多,开放时间很长。理想情况下,您应该做的是与应用程序的所有部分共享连接作为持久连接。 C# 驱动程序应该足够聪明,可以自己完成此操作,这样它就不会创建太多连接,因为它在内部使用“连接池”,甚至可以重用连接。文档说:“与服务器的连接在幕后自动处理(连接池用于提高效率)。”

干杯,
德里克

I would leave the connection open as re-creating the connection is costly. Mongo is fine with lots of connections, open for a long time. What you ideally should do is to share the connection with all parts of your application as a persistent connection. The C# driver should be clever enough to do this itself, so that it does not create too many connections, as internally it uses "connection pooling" that makes it even re-use connections. The docs say: "The connections to the server are handled automatically behind the scenes (a connection pool is used to increase efficiency)."

cheers,
Derick

苄①跕圉湢 2025-01-10 07:46:57

您不需要调用 Connect 或 Disconnect

C# 驱动程序有一个连接池来有效地使用与服务器的连接。无需调用Connect或Disconnect;只需让驱动程序处理连接(调用 Connect 是无害的,但调用 Disconnect 则不好,因为它会关闭连接池中的所有连接)。

http://docs.mongodb.org/ecosystem/tutorial/ csharp-driver 入门/

You Do NOT Need to Call Connect or Disconnect

The C# driver has a connection pool to use connections to the server efficiently. There is no need to call Connect or Disconnect; just let the driver take care of the connections (calling Connect is harmless, but calling Disconnect is bad because it closes all the connections in the connection pool).

http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文