I have the following SP:
CREATE PROCEDURE [dbo].[sp_LockReader]
AS
BEGIN
SET NOCOUNT ON;
begin try
set transaction isolation level serializable
begin tran
select * from teste
commit tran
end try
begin catch
rollback tran
set transaction isolation level READ COMMITTED
end catch
set transaction isolation level READ COMMITTED
END
The table “test” has many values, so “select * from teste” takes several seconds. I run the sp_LockReader at same time in two diferent query windows and the second one starts showing test table contents without the first one terminates.
- Shouldn’t serializeble level forces the second query to wait?
- How do i get the described behaviour?
Thanks
SERIALIZABLE at the most basic means “hold locks for longer”. When you SELECT, the held lock is a shared lock which allows other readers.
If you want to block readers, use
WITH (TABLOCKX)hint to take an exclusive lock where you don’t need SERIALIZABLE. Or XLOCK with SERIALIZABLEIn other words:
TABLOCK = Granularity = what is locked
TABLOCKX = combined
See this question/answer for more info