【问题标题】:NHibernate transaction: Why does this result in committed data?NHibernate 事务:为什么这会导致提交数据?
【发布时间】:2009-10-19 00:58:07
【问题描述】:

以下代码演示了一种误导情况,其中数据 提交到数据库,即使提交从未被调用 交易。

谁能解释一下原因?

[TestFixture]
public class TestFixture
{
        [Test]
        public void Test()
        {
            var config = DoConfiguration();

            using(var factory = config.BuildSessionFactory())
            {
                using (var session = factory.OpenSession())
                {
                    CallSessionContext.Bind(session);

                    using(new TransactionScope())
                    {
                        using (session.BeginTransaction())
                        {
                            var myEntity = session
                               .CreateQuery("from myEntity")
                               .List<MyEntity>()[0];

                            myEntity.Name = "test name";
                        }

                        var myEntity2 = session
                           .CreateQuery("from myEntity")
                           .List<MyEntity>()[0];

                        myEntity2.Name = "test name";

                        session.Flush();
                    }

                    CallSessionContext.Unbind(factory);
                }
            }
        }
} 

【问题讨论】:

    标签: nhibernate transactionscope


    【解决方案1】:

    显式调用session.flush() 会保留您的更改。详细讨论在此post

    【讨论】:

    • 嗨,谢谢,所以我想问题是 - 有没有办法在同一个 TransactionScope 中运行两个 NHibernate 事务,而不会杀死中间的 TransactionScope?
    • 我不够熟悉,不能自信地说,但我怀疑您应该将会话使用与事务范围包装起来,而不是在事务范围之外声明的会话上创建 nHibernate 事务。
    • 另外,也许看看这个看看这个ayende.com/Blog/archive/2006/06/04/…
    • 谢谢。但是,在事务中包装会话与常见的 Session-Per-Web-Request 方法冲突。急……真是一场噩梦
    • 我认为和这个人一样的问题:stackoverflow.com/questions/1518855/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2013-02-25
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多