在Nhibernate中,您可以通过在BeginRequest期间创建会话并在EndRequest处关闭来启动会话
public class Global: System.Web.HttpApplication
{
public static ISessionFactory SessionFactory = CreateSessionFactory();
protected static ISessionFactory CreateSessionFactory()
{
return new Configuration()
.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"))
.BuildSessionFactory();
}
public static ISession CurrentSession
{
get{ return (ISession)HttpContext.Current.Items["current.session"]; }
set { HttpContext.Current.Items["current.session"] = value; }
}
protected void Global()
{
BeginRequest += delegate
{
CurrentSession = SessionFactory.OpenSession();
};
EndRequest += delegate
{
if(CurrentSession != null)
CurrentSession.Dispose();
};
}
}
Run Code Online (Sandbox Code Playgroud)
亚音速中的等价物是什么?
我理解的方式,Nhibernate将在endrequest关闭所有连接.
原因:虽然在Subsonic项目中拍摄一些遗留代码时出现问题,但我得到了很多MySQL超时,这表明代码没有关闭连接
MySql.Data.MySqlClient.MySqlException:连接错误:超时已过期.从池中获取连接之前经过的超时时间.这可能是因为所有池连接都在使用中并且达到了最大池大小.生成:Tue,2009年8月11日05:26:05 GMT System.Web.HttpUnhandledException:抛出了类型'System.Web.HttpUnhandledException'的异常.---> MySql.Data.MySqlClient.MySqlException:连接错误:超时已过期.从池中获取连接之前经过的超时时间.这可能是因为所有池连接都在使用中并且达到了最大池大小.在MySql.Data.MySqlClient.MySqlPool.GetConnection()在SubSonic.MySql上的MySql.Data.MySqlClient.MySqlConnection.Open()处SubSonic.MySqlDataProvider.CreateConnection(String newConnectionString)SubSonic.MySqlDataProvider.CreateConnection()at SubSonic.AutomaticConnectionScope..ctor( DataSvider提供者)SubSonic的SubSonic.MySqlDataProvider.GetReader(QueryCommand qry),SubSonic上的SubSonic.DataService.GetReader(QueryCommand cmd).ReadadByParam(String columnName,Object paramValue)
我的连接字符串如下
<connectionStrings>
<add name="xx" connectionString="Data Source=xx.net; Port=3306; Database=db; UID=dbuid; PWD=xx;Pooling=true;Max Pool Size=12;Min Pool Size=2;Connection Lifetime=60" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
小智 5
除非你专门用"SharedDbConnectionScope"包装你的东西,否则它总是一次性拍摄.我以前见过这个 - 特别是在Windows上测试MySQL时 - 问题是MySQL驱动程序有问题并且没有关闭连接.
我能够通过创建一个控制台应用程序和一个基本的阅读器然后循环它来重现这个 - bam.ConnectionPool错误.
我知道,答案不是很多,但你能做些什么.
| 归档时间: |
|
| 查看次数: |
2358 次 |
| 最近记录: |