2010-07-25 65 views
0

當我呼叫FindAllByProperty時,它會在城堡Active Record中調用OnUpdate,這會導致堆棧溢出,因爲我在OnUpdate實例上做了一些重複檢查。考慮下面的代碼。爲什麼它叫OnUpdate?怎麼能阻止它?爲什麼Castle Active Record的FindAllByProperty調用OnUpdate?

protected override void OnUpdate() 
{ 
    if (FindAllByProperty("Title", this.Title).Length > 1) 
     throw new Exception("duplicate Message in update"); 

    base.OnUpdate(); 
} 

回答

2

這裏是什麼可能發生的事情:

  1. 東西在你的應用程序刷新您的會話。
  2. 雖然潮紅,NHibernate的/ ActiveRecord的執行你的OnUpdate()
  3. 的OnUpdate()調用FindAllByProperty()
  4. FindAllByProperty()嘗試運行同一個會話中查詢,但會議還是很髒,所以NHibernate的衝會議。
  5. 回到2.

因此,堆棧溢出。

爲了避免這種情況,嘗試一種新的會話中運行FindAllByProperty():

using (new SessionScope()) 
    if (FindAllByProperty("Title", this.Title).Length > 1) 
    throw new Exception("duplicate Message in update");