锁定Sql表还是使用lock关键字? [英] Lock a Sql table or use lock keyword?

查看:90
本文介绍了锁定Sql表还是使用lock关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个MVC网络服务,其方法是在添加新记录之前必须检查比赛的最大运动员数量。是不可能在Sql表中注册超过x个运动员;我通过查询同一张表,每次都找到这个限制。
由于Web服务是无状态的,在写入表中的新记录之前,我必须读取同一个表以获得限制。

I have a MVC web service with a method that have to check the maximum number of athletes for race, before to add a new record. Is not possible to register in a Sql table more than x number of athletes; i find every time, this limit, by quering the same table. As the web services are stateless, before to write a new record in a table, i have to read the same table to get limit.

问题是并发调用网络服务(不超过限制)。如果我使用带有&;; IsolationLevel.Serializable的TransactionScope,我会在写新记录时锁定表格。在此交易中,首先我获得限制(使用选择),
然后如果未超过限制,我添加新记录(插入)。如果2个进程(WS中的2个调用)是并发的,当我得到限制时,我将遇到死锁错误。对于我在TransactionScope中使用的死锁问题,锁定关键字(首先是
get限制和第二个新的记录)。

The problem is the concurrency call to web service (for not exceed the limit). If i use a TransactionScope with IsolationLevel.Serializable i lock the table when i write new record. Inside this transaction, first i get the limit (with a Select), then if the limit is not exceeded, i add a new record (Insert). If 2 process (2 calls at WS) are concurrent, when i get the limit, i will have a deadlock error. For the problem of deadlock i have used, inside the TransactionScope, the lock keyword (first get the limit and second adda new record).

这种技术是否正确?或者我可以只使用 锁定关键字而不使用  IsolationLevel.Serializable?

Is this technique correct? or can I use only a lock keyword without using IsolationLevel.Serializable?

谢谢

推荐答案

我有一个MVC网络服务

我会认为你有一个ASP.NET WebAPI Web程序。你有MVC Web服务是什么意思?您是否尝试使用AJAX在ASP.NET MVC项目中调用MVC操作方法,将其称为服务?

I would think that you have an ASP.NET WebAPI Web program. What do you mean that you have a MVC Web service? Are you trying to call a MVC action method in a ASP.NET MVC project calling it a service, by using AJAX?

此技术是否正确?或者我可以只使用 锁定关键字而不使用  IsolationLevel.Serializable?

如果您锁定,那么整个事情似乎都不正确或尝试在多用户Web应用程序环境中的任何情况下锁定表。

No the whole thing doesn't seem to be right that you are locking or trying to lock a table under any circumstances in a multi user Web application environment.

您是否正在使用ADO.NET实体框架并尝试使用System.Transaction隔离级别?

Are you using ADO.NET Entity Framework and trying to use a System.Transaction isolation level?


这篇关于锁定Sql表还是使用lock关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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