[英]Nhibernate transaction locks a tabel
我已经开发了使用nHibernate的WCF api。 我是新来的。 我已经使用session.update来处理事务。 我有一个for循环,其中基于选择条件我正在更新记录,即。 如果tabel1中存在A,那么我将更新表,否则将插入新条目。
我收到“无法执行查询”。 尝试在以前通过在表中添加新条目进行更新的表上执行选择查询时。
我的想法是,因为我正在使用session.save(table1),然后尝试从该表中选择条目,但出现错误。 由于session.save暂时锁定了表,因此我无法对该表执行选择查询。
对此有什么解决方案?
更新 :这是我用于在数据库中检查某些字段的for循环:
using (ITransaction tranx = session.BeginTransaction())
{
savefunction();
tranx.Commit();
}
保存功能:
public void savefunction()
{
for (int i = 0; i < dictionary.Count; i++)
{
ICandidateAttachmentManager candidateAttach = new ManagerFactory().GetCandidateAttachmentManager();
CandidateAttachment attach = new CandidateAttachment();
attach = checkCV();
if(attach == null)
{
//insert new entry into table attach
session.save(attach);
}
}
}
checkCV功能:
public void checkCV()
{
using (ICandidateAttachmentManager CandidateAttachmentManager = new ManagerFactory().GetCandidateAttachmentManager())
{
IList<CandidateAttachment> lstCandidateAttachment = CandidateAttachmentManager.GetByfkCandidateId(CandidateId);
if (lstCandidateAttachment.Count > 0)
{
CandidateAttachment attach = lstCandidateAttachment.Where(x => x.CandidateAttachementType.Id.Equals(FileType)).FirstOrDefault();
if (attach != null)
{
return null;
}
else
{
return "some string";
}
}
}
}
如果说对于i = 2,则附加值变为null,即在我向附加表中输入新条目时,在for循环中发生了什么。 然后对于i = 3,当它进入checkCV函数时,在此行出现错误:
IList lstCandidateAttachment = CandidateAttachmentManager.GetByfkCandidateId(CandidateId);
我认为这是因为自从我使用session.save然后试图读取表格内容后,我无法执行查询,并且在提交会话之前表已被锁定。 在beginTransaction和commit之间,与该对象关联的表被锁定。 我该如何实现? 有任何想法吗?
更新:我阅读了一些帖子。 看来我需要为事务设置隔离级别。 但是即使添加后,它似乎也不起作用。 这是我尝试添加的方式:
using (ITransaction tranx = session.BeginTransaction(IsolationLevel.ReadUncommitted))
{
saveDocument();
}
我在您的代码中看不懂的地方是您进入nHibernate会话的位置。
确实,您使用
new ManagerFactory().GetCandidateAttachmentManager();
和
using (ICandidateAttachmentManager CandidateAttachmentManager = new ManagerFactory().GetCandidateAttachmentManager())
所以您的ManagerFactory类为您提供ISession吗?
然后你做:
CandidateAttachment attach = new CandidateAttachment();
attach = checkCV();
但
checkCV() returns either a null or a string ?
最后你永远都不要做
Save()
但反而
SaveOrUpdate()
希望能帮助您解决问题。
随时提供更多细节
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.