如何在ASP.NET应用程序中实现行级锁定? [英] How to implement row level locking in ASP.NET application?

查看:70
本文介绍了如何在ASP.NET应用程序中实现行级锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET应用程序.两个用户同时登录并在网格"页面中,两个用户都选择相同的记录. User1修改一些数据,然后单击提交按钮. User2修改数据,然后单击提交按钮.此时,我想显示一条弹出消息,指出另一个用户(即User1)修改了该数据.单击确定按钮后,我想回滚User2的事务.如何实现这一目标.请指导我.

如果我有任何示例应用程序或代码,那将是很棒的.谢谢.

I''ve an ASP.NET application. Two users logged in simultaneously and in a Grid page both users choose same record. User1 modifies some data and click submit button. User2 modifies data and click submit button. At this time I would like to show a Pop-up message saying that Another User (ie User1) modified that data. Upon clicking Ok button I would like to rollback the User2''s transaction. How to achieve this. Please guide me.

It would be great if I get any sample application or code. Thanks.

推荐答案

可以说您的记录存储在数据库中,版本为

记录ID版本
1000001 2

如果用户1和2选择相同的记录1000001,则您将从数据库中获取记录详细信息以及版本号(在本例中为"2").

因此,在提交记录时,您可以通过在WHERE CONDITION中执行UPDATE WITH VERSION来检查用户提交的版本是否与数据库中的版本完全相同.对于第一个版本号与数据库版本相同的用户,更新将通过并返回1作为更新的记录数.对于第二个用户,更新的记录数将为0.通过更新计数的结果,您可以显示错误消息.

示例查询:
UPDATE RECORD TABLE SET RECORDDETAIL =''TEST'',VERSION = VERSION + 1,其中RECORD_ID = 1000001和VERSION = 2

这是手动执行.如果使用nHibernate,它将需要锁定和并发.
Lets say your record is stored in Database with a version

RECORD ID VERSION
1000001 2

If the user 1 and 2 picks up same record 1000001,you fetch the record details from the database along with the version number in this case ''2''.

So on submitting the record you check whether the version the user submitting is exactly same as what it is in database by doing a UPDATE WITH VERSION in WHERE CONDITION. For the first user whose version number is same as the database version, the update will go through and will return 1 as number of records updated. For the second user number of records updated will be 0.By the result of updated count you can show the error message.

SAMPLE QUERY:
UPDATE RECORD TABLE SET RECORDDETAIL=''TEST'',VERSION=VERSION+1 WHERE RECORD_ID=1000001 and VERSION=2

This is manual implementation. If you use nHibernate it will take of locking and concurrency.


这篇关于如何在ASP.NET应用程序中实现行级锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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