开发者

Is it possible to retain locks read with update lock in serializable, but releasing in case of read committed?

开发者 https://www.devze.com 2023-04-03 08:30 出处:网络
Consider following two transactions T1 begin tran set transaction isolation level read committed select name from foo with (UPDLOCK) where id = 1 --L1

Consider following two transactions

T1

begin tran
set transaction isolation level read committed
select name from foo with (UPDLOCK) where id = 1 --L1
select blah from bar where id = 2 --L2
commit

T2

begin tran
set transaction isolation level serializable
select name from foo with (UPDLOCK) where id = 1 --L3
select blah from bar where id = 2 --L4
commit

I've observed is that the locks acquired by L1 will be held till the end of transaction and are not released, though the transaction says read committed. Where as in case of serializable, lock is held till the end as expected. What I'm looking for is any hint which will will release update lock in case of read committed but keeps lock in case of serializable. Is this possible?

Bottom line, if I run T1 and T2 parallel, and L1 executes first, L3 will have to wait till L1 finishes. As soon L1 is done, L3 can now execute in parallel and doesn't have to wait for开发者_如何学编程 L2 to end.

This is for Microsoft SQL Server 2008.

EDIT: Why? because I'm trying to solve this problem How to hint update lock without duplicating stored procedure code So if I can release update locks if transaction scope is read committed, it should work.


I've observed is that the locks acquired by L1 will be held till the end of transaction and are not released, though the transaction says read committed.

That's because you've used the UPDLOCK hint. From Table Hints:

UPDLOCK Specifies that update locks are to be taken and held until the transaction completes.

Based on your description everything sounds like it's working as designed. There are no "manual" ways to release locks that have been taken out -- the transaction must complete.

Maybe if you could explain what you are trying to accomplish a solution could be found?

0

精彩评论

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

关注公众号