在Nhibernate中缺少对环境事务的支持吗? [英] Missing support for ambient transactions in nhibernate?

查看:84
本文介绍了在Nhibernate中缺少对环境事务的支持吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道NHibernate支持环境事务,因为NHibernate会话在事务范围内加入环境事务.但是,有些奇怪之处,请考虑以下测试:

I know NHibernate supports ambient transactions, because NHibernate sessions enlists in the ambient transactions while inside a transaction scope. However, there are some oddities, consider the following test:

[Test]
public void Transaction_RollsBackTransactionInsideOfAmbientTransaction_AmbientTransactionAborted()
{
    // arrange
    ISessionFactory sessionFactory = SessionFactoryOneTimeInitializer.GetTestSessionFactory();
    ISession session = sessionFactory.OpenSession();
    SessionFactoryOneTimeInitializer.CreateDataBaseSchemaIfRequiredByConfiguration(session);

    using (new TransactionScope())
    {
        using (ITransaction transaction = session.BeginTransaction())
        {
            // act
            transaction.Rollback();
        }

        // assert
        Assert.AreEqual(TransactionStatus.Aborted, Transaction.Current.TransactionInformation.Status); 
    }
}

此测试失败. NHibernate如何确保环境事务不会持久化到数据库?

This test fails. How will NHibernate ensure that the ambient transaction is not persisted to the database?

推荐答案

我比较了解Hibernate如何在Java世界中与JTA一起工作,但是我不是.NET专家.不过您的问题引起了我的注意.

I know relatively well how Hibernate work with JTA in the Java world, but I am not a .NET expert. Your question nevertheless caught my attention.

在Java中,您需要使用JDBC或JTA事务配置Hibernate.在这种情况下,Hibernate返回的Transaction对象将包装绑定到一个数据库连接(JDBC)的事务或包装在线程本地的全局事务.可以使用 UserTransaction#setRollbackOnly ,以确保它永远不会成功提交.但是,最好不要通过Hibernate管理事务,而只使用JTA提供的UserTransaction对象.

In Java, you need to configure Hibernate with either JDBC or JTA transaction. In which case, the Transaction object returned by Hibernate wraps either a transaction that is bound to one database connection (JDBC) or the global transaction that is thread-local. The global thread-local transaction context can be invalidated using UserTransaction#setRollbackOnly, which ensures it will never commit succesfully. It's however preferable to not manage transaction through Hibernate but to use solely the UserTransaction object provided by JTA.

在NHibernate中似乎还是一样,并且有两个事务工厂.一个用于分布式事务还有一个用于本地交易.但是两者都返回AdoTransaction:

This still seems to be the same in NHibernate and there is two transaction factories. One for distributed transactions and one for local transactions. But both return an AdoTransaction:

public ITransaction CreateTransaction(ISessionImplementor session)
{
    return new AdoTransaction(session);
}

在分布式/环境事务中,这似乎不一致.鉴于全局事务上下文不能在.NET中无效(到目前为止,我了解),因此我不明白rollback在这种情况下的工作方式,并且AdoTransaction似乎代表了数据库连接上的事务.

This doesn't seem to be consistent in case of distributed / ambient transactions. I don't see how rollback would work in this case given that the global transaction context can not be invalided in .NET (so far I understand), and AdoTransaction seems to represent a transaction on database connection.

因此,我觉得您的问题的答案是不会",这将说明您的测试失败.这意味着,如果您使用环境事务,则不应通过NHiberate管理事务.就像在Hibernate和JTA中不推荐这样做一样.

So I feel like the answer to your question is "it won't" which would explain that your test fails. This means that you should not manage transaction through NHiberate if you use ambient transaction. Just like it's not a recommended practice with Hibernate and JTA.

编辑

另请参阅以下问题: TransactionScope如何回滚事务?

这篇关于在Nhibernate中缺少对环境事务的支持吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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