【发布时间】:2011-07-08 10:54:54
【问题描述】:
在 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);
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>();
}
(无耻盗取this related question的例子)
在 NHibernate 源代码中,我可以看到它正在检查是否有正在进行的事务(在 SessionImpl.AutoFlushIfRequired 中),但相关方法(SessionImpl.TransactionInProgress)不考虑环境事务 - 不像它的表亲 ConnectionManager.IsInActiveTransaction,它 确实考虑环境事务。
【问题讨论】:
-
感谢上面的详细分析,我已将其添加到工单中,修复应在 NH 4.1.x.x 中。
-
以上代码:有时你无法避免保存然后从同一个事务中读取。 但在您的情况下(在大多数情况下),无需在该事务中进行读取。 另一种可能性是在读取之前执行 session.Flush()。我知道这是 NHibernate 应该做的事情,但是......
标签: nhibernate transactionscope flush distributed-transactions nhibernate-3