如何锁定使用NHibernate锁模式更新前的对象? [英] How to lock objects before update using NHibernate lock modes?

查看:308
本文介绍了如何锁定使用NHibernate锁模式更新前的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,首先请允许我什么,我试图完成。我有一个填充的工作表。并且有一个方法,允许作业数据的更改名为 SaveJob Web服务。该方法检索作业和所有它的数据,运行验证(这需要它们自己的一些分贝查询其他表)上的新数据,然后将其保存到数据库。这有点慢。关于第二个。

So first let me state what I'm trying to accomplish. I have a table that is populated with jobs. And there is a web service with a method that allows for the alteration of job data called SaveJob. This method retrieves the job and all it's data, runs validations (which require some db queries of their own to other tables) on the new data and then saves it back to the database. It's somewhat slow. About a second.

会发生什么事是偶尔两个 SaveJob 通话将作出并拢在同样的工作,他们会跑过来对方。现在,目前这是唯一的Web服务器,但我想有我的解决方案Web场兼容,所以即使我知道如何使用一个单身我preFER来解决这个问题,已经锁定由数据库处理

What happens is occasionally two SaveJob calls will be made close together on the same job and they'll run over each other. Now currently this is the only web server but I would like to have my solution web farm compatible so even though I'm aware of how to solve this issue using a singleton I'd prefer to have the locks handled by the database.

现在的问题是,我可以用NHibernate和SQL Server数据库锁来获得第二SaveJob调用时,它试图读取已经被改变了另一个SaveJob呼叫的工作阻止?

The question is can I use NHibernate and SQL Server database locks to get a second SaveJob call to block when it tries to read a Job that is already being altered by another SaveJob call?

我相信答案是肯定的,但我真的不知道如何去做。我读过关于 ISession.Lock(),我相信我需要的是使用 NHibernate.LockMode.Upgrade

I believe the answer is yes but I'm not really sure how to go about it. I've read the documentation about the ISession.Lock() and I believe what I need is to use the NHibernate.LockMode.Upgrade

我的下一个问题是当这个锁释放?

My next question is when is this lock released?

我认为当事务被提交,它被释放,但我无法找到明确规定此的任何文档。

I assume that it is released when the transaction is committed but I can't find any documentation that explicitly states this.

然而,如果是这样的话,那么有没有办法为我打开另一个交易,并运行一些查询,但不关闭,我开始时,我抓起摆在首位的工作事务?还是我的有无的做到这一切在一个事务中?

However if that's the case then is there a way for me to open another transaction and run some queries without closing the transaction I've started when I grabbed the job in the first place? Or do I have to do this all in one transaction?

推荐答案

首先,我建议不要使用多个线程一个事务,因为ISession的本身不是线程安全的。

First, I would advise against using a single transaction across multiple threads since the ISession itself is not thread safe.

在使用 LockMode.Upgrade 的会议,或者在 session.Get(),NHibernate的将发行一个与(ROWLOCK)语句选择(详细信息,可能取决于数据库类型和配置)当您检索的实体。如果对象是被另一个线程pviously检索$ P $,当前线程将等待,直到锁被释放或者超时。锁本身是释放的时候更新语句对器transaction.commit执行()。使用 LockMode.UpgradeNoWait 提供了类似的功能,但它不会等待,只是在拍摄时锁定失败。

When you use LockMode.Upgrade on the session, or on session.Get(), NHibernate will issue a select with (rowlock) statement (details might depend on database type and configuration) when you retrieve the entity. If the object was previously retrieved by another thread, the current thread will wait until the lock is released or the timeout expires. The lock itself is release when the update statement is executed on transaction.Commit(). Using LockMode.UpgradeNoWait offers similar functionality except it does not wait and simply fails when the lock is taken.

Ayende的帖子还对哪些SQL的NHibernate发出关于一些信息那么Session.lock()

This Ayende's post also has some info on what SQL NHibernate emits on session.Lock().

您可以阅读不同类型的锁在 NHibernate的文档第11章,第6节

You can read on the different types of locks on the NHibernate Documentation Chapter 11, Section 6.

这篇关于如何锁定使用NHibernate锁模式更新前的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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