Nhibernate transaction locks a tabel

白昼怎懂夜的黑 提交于 2019-12-23 05:27:49

问题


I have developed a WCF api which is using nHibernate. I am new to this. I have used session.update to take care of transaction. I have a for loop in which based on select condition I am updating a record ie. If A is present in tabel1 then I am updating the table else inserting a new entry.

I am getting "could not execute query." when trying to execute a select query on a table which was previously being updated by adding a new entry in the table.

What I think is, because I am using session.save(table1) and then trying select entries from that table I am getting an error. Since session.save temporarily locks the table I am not able to execute a select query on that table.

What can be the solution on this?

Update: This the for loop I am using to check in the database for some field:

using (ITransaction tranx = session.BeginTransaction())
{
   savefunction();
   tranx.Commit();
}

Save function:

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 function:

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";
                }
            }
        }
}

What happening here is in the for loop if say for i=2 the attach value comes to null that I am entering new entry into attach table. Then for i=3 when it enters checkCV function I get an error at this line:

IList lstCandidateAttachment = CandidateAttachmentManager.GetByfkCandidateId(CandidateId);

I think it is because since I am using session.save and then trying to read the tabel contents I am unable to execute the query and table is locked till I commit my session. Between the beginTransaction and commit, the table associated with the object is locked. How can I achieve this? Any Ideas?

Update: I read up on some of the post. It looks like I need to set isolation level for the transaction. But even after adding it doesn't seem to work. Here is how I tried to inplement it:

 using (ITransaction tranx = session.BeginTransaction(IsolationLevel.ReadUncommitted))
 {
     saveDocument();
 }

回答1:


something I don't understand in your code is where you get your nHibernate session.

Indeed you use

    new ManagerFactory().GetCandidateAttachmentManager();

and

    using (ICandidateAttachmentManager CandidateAttachmentManager = new ManagerFactory().GetCandidateAttachmentManager())

so your ManagerFactory class provides you the ISession ?

then you do:

    CandidateAttachment attach = new CandidateAttachment();
    attach = checkCV();

but

    checkCV() returns either a null or a string ?

Finally you should never do

    Save()

but instead

    SaveOrUpdate()

Hope that helps you resolving your issue.

Feel free to give more details



来源:https://stackoverflow.com/questions/22957233/nhibernate-transaction-locks-a-tabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!