本文介绍了NHibernate 3.0:TransactionScope 和自动刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 NHibernate 3.0 中,FlushMode.Auto 在仅在环境事务下运行时不起作用(即,不启动 NHibernate 事务).应该吗?

using (TransactionScope scope = new TransactionScope()){ISession session = sessionFactory.OpenSession();MappedEntity entity = new MappedEntity() { Name = "Entity", Value = 20 };session.Save(实体);entity.Value = 30;session.SaveOrUpdate(实体);//这将返回一个实体,当它应该返回 none 时变量列表 = 会话.CreateQuery(来自 MappedEntity,其中 Value = 20").List();}

(从这个相关问题中无耻地窃取的例子)>

在 NHibernate 源代码中,我可以看到它正在检查是否有正在进行的事务(在 SessionImpl.AutoFlushIfRequired 中),但相关方法 (SessionImpl.TransactionInProgress) 没有不考虑环境事务 - 与其表亲 ConnectionManager.IsInActiveTransaction 不同,后者确实考虑环境事务.

解决方案

好消息. 感谢 Jeff Sternal(他很好地发现了问题)我更新了 https://nhibernate.jira.com/browse/NH-3583 多亏了 NH 工作人员,已经有一个修复和拉取请求因此在即将发布的 4.1.xx 版本中将修复此问题.

In NHibernate 3.0, FlushMode.Auto does not work when running under an ambient transaction only (that is, without starting an NHibernate transaction). Should it?

using (TransactionScope scope = new TransactionScope())
{
    ISession session = sessionFactory.OpenSession();
    MappedEntity entity = new MappedEntity() { Name = "Entity", Value = 20 };
    session.Save(entity);

    entity.Value = 30;
    session.SaveOrUpdate(entity);

    // This returns one entity, when it should return none
    var list = session.
               CreateQuery("from MappedEntity where Value = 20").
               List<MappedEntity>();
}

(Example shamelessly stolen from this related question)

In the NHibernate source I can see that's it's checking whether there's a transaction in progress (in SessionImpl.AutoFlushIfRequired), but the relevant method ( SessionImpl.TransactionInProgress) does not consider ambient transactions - unlike its cousin ConnectionManager.IsInActiveTransaction, which does consider ambient transactions.

解决方案

Good news. Thanks to Jeff Sternal (who nicely identified the problem) I updated https://nhibernate.jira.com/browse/NH-3583 and thanks to the NH staff, there's already a fix and a pull request so in the upcoming release 4.1.x.x this ISSUE will be fixed.

这篇关于NHibernate 3.0:TransactionScope 和自动刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 23:54