【发布时间】:2009-08-11 13:26:45
【问题描述】:
在 Nhibernate 中,您通过在 BeginRequest 期间创建会话来启动会话并在 结束请求
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();
};
}
}
Subsonic 中的等价物是什么?
据我了解,Nhibernate 将在 endrequest 时关闭所有连接。
原因:在解决 Subsonic 项目中的一些遗留代码时遇到很多 MySQL 超时,这表明代码没有关闭连接
MySql.Data.MySqlClient.MySqlException: 连接错误:超时。这 超时时间之前过去 从池中获取连接。 这可能是因为所有 池连接正在使用中并且最大 已达到池大小。生成:星期二, 格林威治标准时间 2009 年 8 月 11 日 05:26:05 System.Web.HttpUnhandledException: 类型异常 'System.Web.HttpUnhandledException' 被抛出。 ---> MySql.Data.MySqlClient.MySqlException: 连接错误:超时。 超时时间过去之前 从池中获取连接。 这可能是因为所有 池连接正在使用中并且最大 已达到池大小。在 MySql.Data.MySqlClient.MySqlPool.GetConnection() 在 MySql.Data.MySqlClient.MySqlConnection.Open() 在 SubSonic.MySqlDataProvider.CreateConnection(字符串 新连接字符串)在 SubSonic.MySqlDataProvider.CreateConnection() 在 SubSonic.AutomaticConnectionScope..ctor(DataProvider 提供者)在 SubSonic.MySqlDataProvider.GetReader(QueryCommand qry) 在 SubSonic.DataService.GetReader(查询命令 cmd) 在 SubSonic.ReadOnlyRecord`1.LoadByParam(字符串 列名,对象参数值)
我的连接字符串如下
<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>
【问题讨论】: