Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Rowlock and lock escalation problem

Author  Topic 

kensai
Posting Yak Master

172 Posts

Posted - 2011-02-04 : 07:12:11
I'm trying to use rowlock to prevent certain rows being updated while it's running but the problem is that I can't use the tables at all while running this:


set transaction isolation level repeatable read;
go

begin try
begin transaction;

insert into tableB with(rowlock)
select * from tableA with(rowlock) where status = 1

commit transaction;
end try
begin catch
if xact_state() <> 0
begin
rollback transaction;
end
end catch;


I ran an insert for both tables while this transaction is running and they both waited for it to commit. What should I do to lock certain rows for read and delete but keep the tables usable?

For the record, the select results tens of thousands of records. The tables have clustered primary keys and status column has an index.

Sachin.Nand

2937 Posts

Posted - 2011-02-04 : 08:26:11
Maybe you can use nolock hint when you want to read the table in some other session.But ofcourse it will be a dirty read and consistency of the data cant be guaranteed.

PBUH

Go to Top of Page

kensai
Posting Yak Master

172 Posts

Posted - 2011-02-07 : 04:09:35
quote:
Originally posted by Sachin.Nand

Maybe you can use nolock hint when you want to read the table in some other session.But ofcourse it will be a dirty read and consistency of the data cant be guaranteed.

PBUH





Well that's something I certainly don't want to do. Isn't there any way to prevent rowlock locking the whole table?
Go to Top of Page

DaleTurley
Yak Posting Veteran

76 Posts

Posted - 2011-02-07 : 04:14:36
Or READPAST.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-02-07 : 04:39:00
quote:
Isn't there any way to prevent rowlock locking the whole table?
You can read smaller chunks of data so that lock escalation will be prevented, but I don't there there are any ways to prevent lock escalation. It is however possible to read the table if you use the READ COMITTED SNAPSHOT isolation level on the *reading* spids ->

http://msdn.microsoft.com/en-us/library/ms188277.aspx

- Lumbago
My blog-> www.thefirstsql.com
Go to Top of Page

kensai
Posting Yak Master

172 Posts

Posted - 2011-02-08 : 09:48:51
quote:
Originally posted by Lumbago

quote:
Isn't there any way to prevent rowlock locking the whole table?
You can read smaller chunks of data so that lock escalation will be prevented, but I don't there there are any ways to prevent lock escalation. It is however possible to read the table if you use the READ COMITTED SNAPSHOT isolation level on the *reading* spids ->

http://msdn.microsoft.com/en-us/library/ms188277.aspx

- Lumbago
My blog-> www.thefirstsql.com



READ COMMITTED SNAPSHOT seems to be enabled on database level which I would hesitate to do so. I could try SNAPSHOT isolation level though. Thanks for the tip.
Go to Top of Page
   

- Advertisement -