开发者

insert exclusive locking

开发者 https://www.devze.com 2023-01-01 12:36 出处:网络
I have thought about the following SQL statements: INSERT INTO A(a1, a2) SELECT b1, udf_SomeFunc(b1) FROM B

I have thought about the following SQL statements:

INSERT INTO A(a1, a2)
SELECT b1, udf_SomeFunc(b1)
FROM B

Where udf_SomeFunc makes a select on table A. As I 开发者_如何学Pythonunderstand, first, a shared lock is set on A (I am talking just about table A now), then, after this lock is released, an exclusive lock is obtained to insert the data. The question is: is it possible, that another transaction will get the exclusive lock on table A, just before the current transaction takes its exclusive lok on A?


Food for thought

create table test(id int)

insert test values(1)
GO

Now in one window run this

begin tran


insert into test
select * from test with (holdlock, updlock)
waitfor delay '00:00:30'
commit

while that is running open another connection and do this

begin tran

insert into test
select * from test with (holdlock, updlock)
commit

as you can see the second insert doesn't happen until the first transaction is complete

now take out the locking hints and observer the difference

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号