[英]Nhibernate returns wrong results
我有這個代碼:
lock (m_session)
{
var _result = m_session.Query<StaticContainerStorage>().Where(c => c.StorageId == storageName && c.ContainerId == null).FirstOrDefault();
if (!String.IsNullOrEmpty(_result.ContainerId))
throw new Exception();
if (_result == null)
{
_result = new StaticContainerStorage(storageName, 0);
AddContainer(_result);
}
return _result;
}
這導致此查詢:
select TOP (1) mfccontain0_.ID as ID0_,
mfccontain0_.StorageId as StorageId0_,
mfccontain0_.StorageIndex as StorageI3_0_,
mfccontain0_.ContainerId as Containe4_0_
from dbo.[MfcContainerStorage] mfccontain0_
where mfccontain0_.StorageId=@p0 and (mfccontain0_.ContainerId is null)
返回正確的行, 但是 ,返回的對象將其屬性ContainerId設置為一個非空的值,以便拋出異常。 這里發生了什么? 我有多個線程訪問該方法,這就是它被鎖定到(單個)會話的原因。
有任何想法嗎?
編輯在我在查詢之前添加m_session.Flush()
之后問題似乎消失了。 仍然不知道出了什么問題。
您正在使用會話鎖定,這表示您要么在應用程序的生命周期中使用一個會話,或者您正在重用不是一個好習慣的會話。
您必須手動刷新以允許會話從先前的更改更新,刷新將發出未完成的插入和更新。
您應該在會話事務中包裝所有查詢和更新。 在提交時,它將自動刷新,但它也將允許緩存,批處理和將來的查詢正常工作。
nhibernate和會話生命周期管理的研究工作單位。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.