如何在NHibernate中嵌套事务? [英] How do I do nested transactions in NHibernate?

查看:98
本文介绍了如何在NHibernate中嵌套事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在NHibernate中进行嵌套事务,如何实现它们?我使用的是SQL Server 2008,因此DBMS肯定是支持的.

Can I do nested transactions in NHibernate, and how do I implement them? I'm using SQL Server 2008, so support is definitely in the DBMS.

我发现,如果尝试这样的操作:

I find that if I try something like this:

using (var outerTX = UnitOfWork.Current.BeginTransaction())
{
    using (var nestedTX = UnitOfWork.Current.BeginTransaction())
    {
        ... do stuff
        nestedTX.Commit();
    }

    outerTX.Commit();
}

然后,到outerTX.Commit()时,事务已变为非活动状态,并导致会话AdoTransaction上出现ObjectDisposedException.

then by the time it comes to outerTX.Commit() the transaction has become inactive, and results in a ObjectDisposedException on the session AdoTransaction.

因此我们是否应该改为创建嵌套的NHibernate会话?还是还有其他一些类可以用来包装事务(我听说过TransactionScope,但是我不确定那是什么)?

Are we therefore supposed to create nested NHibernate sessions instead? Or is there some other class we should use to wrap around the transactions (I've heard of TransactionScope, but I'm not sure what that is)?

我现在正在使用 Ayende的UnitOfWork实现(感谢Sneal).

I'm now using Ayende's UnitOfWork implementation (thanks Sneal).

请原谅这个问题,我还是NHibernate的新手.

Forgive any naivety in this question, I'm still new to NHibernate.

谢谢!

编辑:我发现您可以使用TransactionScope,例如:

EDIT: I've discovered that you can use TransactionScope, such as:

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可以通过某种方式来模拟相同的事务.

However I'm not all that excited about this, as it locks us in to using SQL Server, and also I've found that if the database is remote then you have to worry about having MSDTC enabled... one more component to go wrong. Nested transactions are so useful and easy to do in SQL that I kind of assumed NHibernate would have some way of emulating the same...

推荐答案

NHibernate会话不支持嵌套事务.

NHibernate sessions don't support nested transactions.

以下测试在版本2.1.2中始终为真:

The following test is always true in version 2.1.2:

var session = sessionFactory.Open();
var tx1 = session.BeginTransaction();
var tx2  = session.BeginTransaction();
Assert.AreEqual(tx1, tx2);

您需要将其包装在TransactionScope中以支持嵌套事务.

You need to wrap it in a TransactionScope to support nested transactions.

必须启用MSDTC,否则您将收到错误消息:

MSDTC must be enabled or you will get error:

{"Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool."}

这篇关于如何在NHibernate中嵌套事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆