C# NHibernate中已达到连接池限制
对于使用NHibernate的MVC应用程序,我在尝试正确处理我的工作单元的生命周期时遇到了问题(无论如何,我认为这是我的UoW的问题)。多线程是有可能的,所以它也需要考虑到这一点。我决定尝试让NHibernate管理连接,而不是在依赖项注入器(StructureMap)中进行管理,但现在我遇到了连接池限制达到的问题 为了发布代码,我简化了其中的一些内容,但希望它能帮助我了解我遗漏了什么 NHibernate配置C# NHibernate中已达到连接池限制,c#,asp.net-mvc,nhibernate,connection-pooling,structuremap,C#,Asp.net Mvc,Nhibernate,Connection Pooling,Structuremap,对于使用NHibernate的MVC应用程序,我在尝试正确处理我的工作单元的生命周期时遇到了问题(无论如何,我认为这是我的UoW的问题)。多线程是有可能的,所以它也需要考虑到这一点。我决定尝试让NHibernate管理连接,而不是在依赖项注入器(StructureMap)中进行管理,但现在我遇到了连接池限制达到的问题 为了发布代码,我简化了其中的一些内容,但希望它能帮助我了解我遗漏了什么 NHibernate配置 var cfg = new Configuration() .DataBa
var cfg = new Configuration()
.DataBaseIntegration(x =>
{
x.ConnectionStringName = "ApplicationConnectionStringName";
x.Dialect<CustomMsSql2008Dialect>(); //Inherits from MsSql2008GeographyDialect
x.IsolationLevel = IsolationLevel.RepeatableRead;
x.BatchSize = 100;
})
.CurrentSessionContext<WebSessionContext>()
.Cache(c =>
{
c.Provider<SysCacheProvider>();
c.UseQueryCache = true;
});
通过StructureMap请求开始和结束处理程序
public class StructureMapScopeModule : IHttpModule {
public void Dispose() {
}
public void Init(HttpApplication context) {
context.BeginRequest += (sender, e) =>
{
StructuremapMvc.StructureMapDependencyScope.CreateChildContainer();
var unitOfWork = StructuremapMvc.StructureMapDependencyScope.Container.GetInstance<IUnitOfWork>();
};
context.EndRequest += (sender, e) => {
var unitOfWork = StructuremapMvc.StructureMapDependencyScope.Container.GetInstance<IUnitOfWork>();
unitOfWork.Commit();
HttpContextLifecycle.DisposeAndClearAll();
StructuremapMvc.StructureMapDependencyScope.DisposeChildContainer();
};
}
}
公共类结构MapScopeModule:IHttpModule{
公共空间处置(){
}
公共void Init(HttpApplication上下文){
context.BeginRequest+=(发件人,e)=>
{
StructuremapMvc.StructureMapDependencyScope.CreateChildContainer();
var unitOfWork=StructuremapMvc.StructureMapDependencyScope.Container.GetInstance();
};
context.EndRequest+=(发送方,e)=>{
var unitOfWork=StructuremapMvc.StructureMapDependencyScope.Container.GetInstance();
unitOfWork.Commit();
HttpContextLifecycle.DisposeAndClearAll();
StructuremapMvc.StructureMapDependencyScope.DisposeChildContainer();
};
}
}
现在运行一个查询以获取连接数将提供近125个连接。请处理掉所有打开的连接,这通常是达到池限制的原因 正确实现IDisposable也不像看上去那么简单。。。下面是我正在使用的示例实现(从上面的答案中复制)
很好,您在评论之前测试了它。。。将评论作为答案。我认为问题更多地在于如何处理连接,您还应该在终结器中处理它。。。。我正要发布一个回复,我已经验证了对
DisposeAndClearAll()
的调用是否会级联到容器中的所有项目中,其中包括我的UnitOfWork
,这将处理会话,这将关闭连接。然后我想了想,又去测试了一遍,发现你是对的。显然,StructureMap中的DisposeAndClearAll()
不会传播到子容器,而且DisposeAndClearContainer()
也没有处理容器中的项目。如果你想快速给出这个问题的答案,我会接受你的回答。
public class StructureMapScopeModule : IHttpModule {
public void Dispose() {
}
public void Init(HttpApplication context) {
context.BeginRequest += (sender, e) =>
{
StructuremapMvc.StructureMapDependencyScope.CreateChildContainer();
var unitOfWork = StructuremapMvc.StructureMapDependencyScope.Container.GetInstance<IUnitOfWork>();
};
context.EndRequest += (sender, e) => {
var unitOfWork = StructuremapMvc.StructureMapDependencyScope.Container.GetInstance<IUnitOfWork>();
unitOfWork.Commit();
HttpContextLifecycle.DisposeAndClearAll();
StructuremapMvc.StructureMapDependencyScope.DisposeChildContainer();
};
}
}
using System;
namespace AlgoSys.Common.SharedUtils
{
public abstract class Disposable : IDisposable
{
private bool _disposed;
// Dispose() calls Dispose(true)
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// NOTE: Leave out the finalizer altogether if this class doesn't
// own unmanaged resources itself, but leave the other methods
// exactly as they are.
~Disposable()
{
// Finalizer calls Dispose(false)
Dispose(false);
}
// The bulk of the clean-up code is implemented in Dispose(bool)
protected virtual void Dispose(bool disposing)
{
if(_disposed) return;
if (disposing)
{
OnDispose();
}
_disposed = true;
}
protected abstract void OnDispose();
}
}