且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET应用程序中实现行级锁定?

更新时间:2022-11-06 18:47:07

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

记录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.