行级锁定 [英] Row-Level Locking

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

问题描述

如何在SQL Server上进行行级锁定?


谢谢,

Nid

How do I do row-level locking on SQL Server?

Thanks,
Nid

推荐答案

Nid(kp***@purdue.edu)写道:
Nid (kp***@purdue.edu) writes:
如何在SQL Server上进行行级锁定?
How do I do row-level locking on SQL Server?




问题是非特定的,以获得一个好的答案。如果您只想要

来确保表访问通常使用行锁定,那么确保您在WHERE子句中包含的列上有索引。 br />
如果你说:


UPDATE tbl SET p = 0 WHERE col = 12


并且没有索引在tbl.col上,然后此查询可能会锁定整个

表,直到事务提交为止。如果col上有索引,SQL

服务器可以直接找到该行,只需要锁定该行。


如果你的意思是别的,请更具体。

-

Erland Sommarskog,SQL Server MVP, es * ***@sommarskog.se


SQL Server SP3的联机书籍
http://www.microsoft.com/sql/techinf...2000/books.asp


Hello Erland,


首先,感谢您的回复。我必须承认我只知道一点点关于SQL Server的问题。


无论如何,我有一个ColdFusion应用程序从表中检索记录

称为ASSIGNMENT。目前,多个用户可以同时更新相同的作业

(这真的很糟糕)虽然我使用了


SELECT * FROM ASSIGNMENT WITH(ROWLOCK)

WHERE Assignment_ID =< value>


Assignment_ID是一个身份编号,它是此表的主键。

我认为SQL Server会自动为这个人创建一个索引,因为它是主键的



我正在寻找像MS这样的东西编辑行被锁定的访问表格

自动。我不想通过应用程序进行锁定,但如果有办法(我希望),那么

表本身。


我发现一些网站谈论EXEC sp_tableoption''< table name>'',''插入

row lock'',''true''。我想这与我正在寻找的东西相同,但是

适用于6.5,而不是2000. :(


我希望我不要混淆你。

Nid

" Erland Sommarskog" es **** @ sommarskog.se>写在留言中

news:Xn ********************* @ 127.0.0.1 ...
Hello Erland,

First of all, thank you for the response. I have to confess that I know just
a little bit about SQL Server.

Anyway, I have a ColdFusion application retrieving a record from a table
called ASSIGNMENT. Currently multiple users can update the same assignment
at the same time (which is really bad) although I used

SELECT * FROM ASSIGNMENT WITH (ROWLOCK)
WHERE Assignment_ID = <value>

Assignment_ID is an identity number and it''s the primary key of this table.
I think SQL Server automatically created an index for this guy since it''s
the primary key.

I''m looking for anything like MS Access form which an editing row is locked
automatically. I don''t want to do the lock via the application, but the
table itself if there is a way (I hope).

I found some sites talking about EXEC sp_tableoption ''<table name>'', ''insert
row lock'', ''true''. I guess it''s the same thing I''m looking for, but it
works on 6.5, not 2000. :(

I hope I don''t confuse you.
Nid
"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn*********************@127.0.0.1...
Nid(kp *** @ purdue。 edu)写道:
Nid (kp***@purdue.edu) writes:
如何在SQL Server上进行行级锁定?
How do I do row-level locking on SQL Server?



问题是非特定的,以获得一个好的答案。如果你所有的话想要确保表访问一般使用行锁定,确保你在WHERE子句中包含的列上有索引。
如果你说:

UPDATE tbl SET p = 0 WHERE col = 12
并且tbl.col上没有索引,那么此查询可能会锁定整个
表,直到事务提交为止。 SQL上有一个索引,SQL
服务器可以直接找到该行,只需要锁定该行。

如果你的意思是其他的,请更具体。

-
Erland Sommarskog,SQL Server MVP, eswers@sommarskog.se

SQL Server SP3的在线书籍
http://www.microsoft.com/sql/techinf...2000/books.asp



Nid,


试图长时间保持这样的行锁不是一个好主意。 >
一段时间。它可能导致阻止其他类型的操作,并且会损坏系统的可扩展性。


相反,你将不得不创建你自己的锁定方案。这可能

涉及表格上的一列,表明行是否被锁定(或者是否由
为谁),一列表明它被锁定,以及一个作业到期锁定在

a定期。您可能还需要考虑TIMESTAMP列。即使锁定已过期,如果没有其他

用户更新了相关行,这个

将允许您仍然进行更新。您可以将时间戳发送回应用程序,然后在应用程序写入新数据之前,它可以检查确定时间戳是否相同。

确保时间戳相同。


最后,你应该考虑使用INSTEAD OF触发器来更新

表,以确保每个应用都遵循锁定规则,这样你就不会

必须复制许多存储过程或应用程序中的代码。


" Nid" < KP *** @ purdue.edu>在留言中写道

news:ch ********** @ mozo.cc.purdue.edu ...
Nid,

It''s not a good idea to attempt to hold row locks like this for a long
period of time. It can cause blocking of other types of operations and will
help to ruin scalability of a system.

Instead, you''re going to have to create your own locking scheme. This might
involve a column on your table indicating whether a row is locked (or by
whom), a column indicating when it was locked, and a job to expire locks on
a regular basis. You might also want to consider a TIMESTAMP column. This
will allow you to still do updates even if the lock has expired, if no other
users have updated the row in question. You can send the timestamp back to
the application and before the app writes the new data, it can check to make
sure the timestamp is the same.

Finally, you should consider using an INSTEAD OF trigger for UPDATEs to the
table, to ensure that every app follows the locking rules, so that you won''t
have to duplicate the code in many stored procedures or applications.

"Nid" <kp***@purdue.edu> wrote in message
news:ch**********@mozo.cc.purdue.edu...

我是'我正在寻找像MS Access表单一样的编辑行是
自动锁定。我不想通过应用程序进行锁定,但是如果有办法(我希望),
表本身。

I''m looking for anything like MS Access form which an editing row is locked automatically. I don''t want to do the lock via the application, but the
table itself if there is a way (I hope).



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

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