Subsonic如何处理连接?

时间:2009-08-11 13:26:45

标签: .net subsonic

在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();
        };
    }
}

Subsonic中的等价物是什么?

我理解的方式,Nhibernate将在endrequest关闭所有连接。

原因: 虽然在Subsonic项目中解决了一些遗留代码问题,但我得到了很多MySQL超时,建议代码没有关闭连接

  

MySql.Data.MySqlClient.MySqlException:   错误连接:超时已过期。该   在此之前经过了超时时间   从池中获取连接。   这可能是因为所有   汇集连接正在使用中,最大值   达到了游泳池大小。生成:星期二,   2009年8月11日05:26:05 GMT   System.Web.HttpUnhandledException:   类型异常   'System.Web.HttpUnhandledException'   被扔了。 --->   MySql.Data.MySqlClient.MySqlException:   错误连接:超时已过期。   超时时间过去之前   从池中获取连接。   这可能是因为所有   汇集连接正在使用中,最大值   达到了游泳池大小。在   MySql.Data.MySqlClient.MySqlPool.GetConnection()   在   MySql.Data.MySqlClient.MySqlConnection.Open()   在   SubSonic.MySqlDataProvider.CreateConnection(字符串   newConnectionString)at   SubSonic.MySqlDataProvider.CreateConnection()   在   SubSonic.AutomaticConnectionScope..ctor(DataProvider的   提供者)   SubSonic.MySqlDataProvider.GetReader(QueryCommand   qry)at   SubSonic.DataService.GetReader(QueryCommand   cmd)at   SubSonic.ReadOnlyRecord`1.LoadByParam(字符串   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>

2 个答案:

答案 0 :(得分:5)

除非你专门用“SharedDbConnectionScope”包装你的东西,否则它总是一次性拍摄。我以前见过这个 - 特别是在Windows上测试MySQL时 - 问题是MySQL驱动程序有问题并且没有关闭连接。

我能够通过创建一个控制台应用程序和一个基本读取器然后循环它来重现这个--bam。 ConnectionPool错误。

我知道答案不是很多,但你能做什么。

答案 1 :(得分:4)

在使用SubSonic之前,我遇到了同样的错误。我们有一段Javascript,每隔2分钟左右就会对服务器进行一次操作只是为了让我们的会话保持活跃状态​​,当它ping到服务器时,会调用我们的会员资格来使用DataReader。无论如何,我们并没有关闭读者,而且即使只有少数几个用户,这个小小的活着脚本也会慢慢吞噬我们所有160个允许的连接。您可能想要检查是否以这种方式使用SubSonic(从查询返回IDataReader,然后不处理它们)

相关问题