简体   繁体   English

NHibernate回滚似乎不起作用

[英]NHibernate rollback does not seem to work

My question is simple. 我的问题很简单。 I have the below code and user is still inserted. 我有以下代码,并且仍然插入了用户。 When I check the database just after SaveOrUpdate (and before the rollback) I see user is already inserted. 当我在SaveOrUpdate之后(以及回滚之前)检查数据库时,我看到用户已经插入。 It's like flush mode and transaction not working. 就像刷新模式和事务不起作用一样。 Where am I going wrong? 我要去哪里错了?

using (var session = sessionFactory.OpenSession())
{
    session.FlushMode = FlushMode.Never;

    using (var tran = session.BeginTransaction())
    {
        var user = CreateUser();

        session.SaveOrUpdate(user);

        tran.Rollback();
    }
}

If we check "just after SaveOrUpdate " ... we are still inside of one transaction. 如果我们仅在“ SaveOrUpdate之后”进行检查SaveOrUpdate ...仍在一项事务中。 Lot of stuff could happen before final decision (commit or rollback). 在做出最终决定(提交或回滚)之前,可能会发生很多事情。

And one of the operation would be to decide if object should be Created or Updated . 该操作之一是确定对象应该Created还是Updated So in case, that the ID generator is set to native/identity (eg SQL server) - NHibernate must execute the INSERT to get the ID. 因此,如果将ID生成器设置为本机/身份(例如SQL Server),则NHibernate必须执行INSERT才能获取ID。 Lot of operation could be postponed, but to receive the ID - there is no way to wait. 可以推迟很多操作,但要接收ID-无法等待。

So most likely your ID need to be obtained from DB.. and that's why INSERT happens. 因此,很可能需要从数据库获取您的ID。这就是为什么发生INSERT的原因。 BUT other stuff won't be written into DB, until Flush() is called... So, I wouldn't mark described behaviour as something special. 但是在调用Flush()之前,其他内容将不会写入数据库。因此,我不会将描述的行为标记为特殊行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM