如何确保MongoDB连接的处置(DB Hanging)

发布于 2024-11-07 23:22:20 字数 934 浏览 0 评论 0原文

首先是一些上下文:我有一个 MVC3 .net 项目,为了简洁起见,其设置如下:

控制器 - 实例化服务对象(如下所述) - 使用服务从 mongo 检索数据库记录(例如 _service.GetPerson(id)) - 通过域名查看

服务 - 实例化 MongoRepository(如下所述) - 调用方法来检索数据库记录(例如 _mongoRepository.Single(c => c.Id == id))

MongoRepository : IDisposable - 构造函数 (_server = Mongo.Create(ConfigurationManager.ConnectionStrings["mongodb"].ConnectionString)) - 单一方法(下) - Dispose 方法

public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
        {
            return _server.GetCollection<T>().AsQueryable()
                        .Where(expression).SingleOrDefault();
        }

现在,这是我的问题,正如您所看到的 MongoRepository 实现了 IDisposable 接口,并且我相信确保调用 Dispose 的最佳方法是使用“using”块,但是

1) 当/那应该在哪里?它应该在服务层中,在调用 mongoRepository.Single 方法的方法内部吗?

2)MongoRepository应该什么时候实例化?

如果需要更多代码来回答,请告诉我,我试图保持简短。提前致谢。

First some context: I have an MVC3 .net project which, for the sake of brevity, is set up something like the following:

Controller
- Instantiates a service object (described below)
- Uses service to retrieve db record from mongo (e.g. _service.GetPerson(id))
- Passes domain to view

Service
- Instantiates MongoRepository (described below)
- Calls method to retrieve db record (e.g. _mongoRepository.Single(c => c.Id == id))

MongoRepository : IDisposable
- Constructor (_server = Mongo.Create(ConfigurationManager.ConnectionStrings["mongodb"].ConnectionString))
- Single method (below)
- Dispose method

public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
        {
            return _server.GetCollection<T>().AsQueryable()
                        .Where(expression).SingleOrDefault();
        }

Now, this is my question, as you can see MongoRepository implements the IDisposable interface, and I believe the best way to ensure Dispose is called is with the 'using' block, but

1) when/where should that be? Should it be in the Service layer, inside the method calling the mongoRepository.Single method?

2) when should the MongoRepository be instantiated?

If more code is necessary to answer please let me know, I was trying to keep it short. Thanks in advance.

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

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

发布评论

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

评论(1

薆情海 2024-11-14 23:22:20

1)听起来您正在实例化并使用 Service 类中的存储库。因此,

var _mongoRepository = new MongoRepository(..);
_mongoRepository.Single(...);

如果您

using (var _mongoRepository = new MongoRepository(..))
{
    _mongoRepository.Single(..);
}

将 Mongo 调用包装在 MongoRepository 类中,那么请确保 Dispose 方法正确清理 _server 连接(关闭/断开连接/其他) 。看起来您正在构造函数中创建连接,在方法中使用它但从未关闭它。

2)根据我的经验,最好尽可能晚地打开数据库连接并尽早关闭它 - 而不是打开它并针对不同的查询多次使用它。

.NET MongoDB 连接最佳实践?<上有关于 MongoDB (C#) 连接的很好的讨论。 /a>

1) It sounds like you are instantiating and using the Repository in the Service class. So instead of

var _mongoRepository = new MongoRepository(..);
_mongoRepository.Single(...);

you would have

using (var _mongoRepository = new MongoRepository(..))
{
    _mongoRepository.Single(..);
}

If you are wrapping Mongo calls in the MongoRepository class then make sure that the Dispose method properly cleans up the _server connection (close/disconnect/whatever). It looks like you are creating the connection in the constructor, using it in the method but never closing it.

2) From my experience it is preferable to open a DB connection as late as possible and close it as early as possible - rather than opening it and using it several times for different queries.

There's a good discussion on MongoDB (C#) connections at .NET best practices for MongoDB connections?

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