NHibernate的交易锁定一个TABEL [英] Nhibernate transaction locks a tabel

查看:160
本文介绍了NHibernate的交易锁定一个TABEL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发出使用NHibernate的一个WCF API。我是新来这个。我已经使用session.update采取交易的照顾。我有一个for循环中,根据选择的条件,我更新记录IE浏览器。如果A是目前在tabel1那么我更新表中插入别的一个新的条目。



我收到无法执行查询。试图执行上这是以前被添加在表中一个新条目更新的表的SELECT查询时。



我觉得是什么,因为我使用的会话。保存(表1),然后从该表尝试,我得到一个错误选择项。由于session.save暂时锁定表,我不能执行该表的SELECT查询。



有什么可以对这个?

$ B解决方案
$ b

更新
这样的循环。我正在使用的数据库的一些字段,检查:

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



保存功能:

 公共无效savefunction()
{
的for(int i = 0; I< dictionary.Count;我++)
{
ICandidateAttachmentManager candidateAttach =新ManagerFactory()GetCandidateAttachmentManager()。
CandidateAttachment附加=新CandidateAttachment();
附加= checkCV();
如果(附== NULL)
{
//将新进入表附上
session.save(附后);
}
}
}



checkCV功能:

 公共无效checkCV()使用(ICandidateAttachmentManager CandidateAttachmentManager =新ManagerFactory()。GetCandidateAttachmentManager())$ 
{
b $ b {
&IList的LT; CandidateAttachment> lstCandidateAttachment = CandidateAttachmentManager.GetByfkCandidateId(CandidateId);
如果(lstCandidateAttachment.Count大于0)
{
CandidateAttachment附加= lstCandidateAttachment.Where(X => x.CandidateAttachementType.Id.Equals(文件类型))FirstOrDefault()。
如果(附上!= NULL)
{
返回NULL;
}
,否则
{
返回一些字符串;
}
}
}
}



什么这里发生在for循环,如果说对于i = 2的附加价值来,我进入新进入附加表空。然后,对于i = 3,当它进入checkCV功能我在这行得到一个错误:




IList的lstCandidateAttachment =
CandidateAttachmentManager.GetByfkCandidateId (CandidateId);




我想这是因为自从我使用session.save,然后尝试读取TABEL内容我不能执行查询和表被锁定,直到我承诺我的会议。所述的BeginTransaction和提交之间,与该对象相关联的表被锁定。我怎样才能做到这一点? ?任何想法



更新:
我的一些帖子的阅读起来。它看起来像我需要设置的事务隔离级别。但是,即使加入后,它似乎并没有工作。这里是我试图inplement它:

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


解决方案

东西我不明白在你的代码是你得到你的nHibernate会话。



事实上,你用

 新ManagerFactory()GetCandidateAttachmentManager()。 

 使用(ICandidateAttachmentManager CandidateAttachmentManager =新ManagerFactory()。GetCandidateAttachmentManager())

所以你ManagerFactory ?类为您提供的ISession的



那么你做的:

  CandidateAttachment附加=新CandidateAttachment(); 
附加= checkCV();



  checkCV()返回无论是空或字符串? 



最后,你不应该做的。



 保存()

而是

  saveOrUpdate()方法

。希望可以帮助你解决问题。



随意透露更多的细节。


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();
 }

解决方案

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

这篇关于NHibernate的交易锁定一个TABEL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆