2009-08-11 74 views
3

在NHibernate中,你用的BeginRequest中創建它啓動一個會話,並收於 EndRequestSubsonic如何處理連接?

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

什麼是在亞音速等價?

我明白,Nhibernate會關閉endrequest中的所有連接。

原因:雖然麻煩一些拍攝遺留代碼在亞音速項目,我得到了很多的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() 在 MySql.Data.MySqlClient.MySqlConnection.Open() 在 SubSonic.MySqlDataProvider.CreateConnection(字符串 newConnectionString)在 SubSonic.MySqlDataProvider.CreateConnection() 在 SubSonic.AutomaticConnectionScope..ctor(DataProvider的 提供商)處 SubSonic.DataService.GetReader(QueryCommand CMD) SubSonic.MySqlDataProvider.GetReader(QueryCommand QRY)在 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> 

回答

5

它總是一個一次性拍攝,除非你明確了「SharedDbConnectionScope」包裝你的東西。我以前見過這個 - 特別是在Windows上測試MySQL時 - 並且問題在於MySQL驅動程序有問題,並且沒有關閉連接。

我能夠通過創建一個控制檯應用程序和一個基本的閱讀器,然後循環播放它 - 巴姆。 ConnectionPool錯誤。

我知道答案並不多,但你可以做些什麼。

+0

搶,我不明白這部分=「它總是一個一次性拍攝,除非你特別是用「SharedDbConnectionScope」包裝你的東西。「我該怎麼做呢?。你能指點我一個例子嗎? – 2009-08-12 01:08:31

+0

看看這裏http://subsonicproject.com/docs/3.0_Transactions – 2009-08-12 19:31:46

+0

所以,如果我不想打開和關閉很多連接,我必須使用這個SharedDbConnectionScope?這似乎很重要!如果我不關心交易,是否還需要TransactionScope? – Rory 2010-01-17 10:44:16

4

我已經遇到了錯誤使用SubSonic之前相同的風格。我們有一段Javascript腳本每隔2分鐘左右連接一次服務器,以保持會話活躍,並在服務器調用了使用DataReader的成員資格調用。無論如何,長話短說,我們並沒有關閉讀者,即使只有少數用戶,小小的活着腳本也會緩慢吞噬我們所有160個允許的連接。您可能要檢查,看看是否你以這種方式使用亞音速(從查詢返回的IDataReader,然後不處置它們)