如何确保MongoDB连接的处置(DB Hanging)
首先是一些上下文:我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)听起来您正在实例化并使用 Service 类中的存储库。因此,
如果您
将 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
you would have
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?