在NHibernate 3.0中,当仅在环境事务下运行(即未启动NHibernate事务)时,
FlushMode.Auto
无法工作。是否应该这样?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
,它考虑环境事务。