能做些什么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