能做些什麼NHibernate的嵌套事務,以及如何實現它們?我正在使用SQL Server 2008,所以支持絕對在DBMS中。如何在NHibernate中嵌套事務?
我覺得,如果我嘗試這樣:
using (var outerTX = UnitOfWork.Current.BeginTransaction())
{
using (var nestedTX = UnitOfWork.Current.BeginTransaction())
{
... do stuff
nestedTX.Commit();
}
outerTX.Commit();
}
那麼到時候談到outerTX.Commit()
交易已經變得不活躍,並導致在會話AdoTransaction一個的ObjectDisposedException。
難道我們因此應該代替創建嵌套NHibernate的會議?還是有一些其他類應該用來包裝事務(我聽說過TransactionScope,但我不確定那是什麼)?
現在,我使用Ayende's UnitOfWork implementation(感謝Sneal)。
原諒在這個問題上任何的幼稚,我還是新來的NHibernate。
謝謝!
編輯:我發現,你可以使用的TransactionScope,如:
using (var transactionScope = new TransactionScope())
{
using (var tx = UnitOfWork.Current.BeginTransaction())
{
... do stuff
tx.Commit();
}
using (var tx = UnitOfWork.Current.BeginTransaction())
{
... do stuff
tx.Commit();
}
transactionScope.Commit();
}
但是我沒有那麼興奮,這一點,因爲它鎖定在我們使用SQL Server和我也發現,如果數據庫是遠程的,那麼你不得不擔心MSDTC已啓用......另外一個組件出錯了。嵌套事務在SQL中非常有用且容易實現,我認爲NHibernate會採用某種方式來模擬相同的...
你已經能夠找到答案?如何最終做嵌套事務? – learning 2010-07-20 10:23:58
@ user281180,有點。我找不到真正做到的方法,但你可以近似體驗。在這裏發表了博客:http://blog.constructionhive.com/2010/07/22/nested-transactions-and-nhibernate/ – Gavin 2010-07-22 00:19:12