SQL Server >> Concurrent access,, Locks, and Deadlocks

by YZ » Sat, 18 Sep 2004 01:12:00 GMT

We use the following sp in our VB applications.

The basic logic in this sp is 1) read a row, 2)marked the row is
"checked out), 3) write the same row to another table.

This sp will be executed by many users at the same time. The current
logic we built having slow response, but won't lead to any
locking/deadlocks. However, if we add transaction to make sure the row
we read (you do not want others to have same row) is locked, then we
starting have deadlock/lock problem. Any suggestions to improve the
performance without risk of deadlocks (assuming we have good indexes on
the table)?

Thank you very much for your expertise.


CREATE PROCEDURE sp_get_dcn_sql
@UserID AS char(8),
@ProfileID as int
AS

Set nocount on

declare @tempWhere as varchar(2000), @tempOrderBy as varchar(1000),
@sqlstr as nvarchar(3000)
declare @tempDCN as varchar(16), @tempST as char(2), @tempDept as
char(5)
declare @flagGoodDCN as char(1)

create table #tempDCN (tempDCN varchar(16) NULL, tempST char(2) NULL,
tempDept char(5) NULL)

select @tempWhere = ProfileWhere, @tempOrderBy = ProfileOrderBy from
tblYZProfileText where ProfileID = @ProfileID

set @sqlstr = 'select top 1 DCN, CO_Cd, Dept from tblYZInventoryDetail
where ' +
@tempWhere + ' and (check_out is null or check_out = ''N'' or
check_out <> ''Y'') '

if ltrim(rtrim(@tempOrderBy)) is not null
set @sqlstr = @sqlstr + ' order by ' + @tempOrderBy

set @flagGoodDCN = ' '

while @flagGoodDCN <> 'Y' --loop to find the next untouched DCN
begin
insert into #tempDCN exec sp_executesql @sqlstr

select @tempDCN = tempDCN, @tempST = tempST, @tempDept = tempDept from
#tempDCN

if @tempDCN is not null
begin

update tblYZInventoryDetail set check_out = 'Y'
where DCN = @tempDCN and Co_Cd = @tempST and Dept = @tempDept
insert into tblYZWorkedClaims (DCN, StateID, Dept, StartTime,
WorkedUser)
select @tempDCN, @tempST,@tempDept,getdate(),@UserID
if @@error = 0 --catch PK violation
set @flagGoodDCN = 'Y'

else
truncate table #tempDCN --go loop
end
else
begin
break
end

end

select tempDCN as DCN, tempST as Co_Cd, tempDept as Dept from #tempDCN

drop table #tempDCN

Set nocount off
GO


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


SQL Server >> Concurrent access,, Locks, and Deadlocks

by Greg Linwood » Mon, 20 Sep 2004 19:45:34 GMT


'm guessing that you're experiencing conversion deadlocks when the read
locks taken by the select later need to be upgraded to exclusive locks for
the update.

One common solution for this problem is to take update locks on the select
which should ease the deadlock problem when you introduce the transaction
statement, eg:

select @tempWhere = ProfileWhere, @tempOrderBy = ProfileOrderBy
from tblYZProfileText WITH (UPDLOCK)
where ProfileID = @ProfileID

You can read up on this locking hint in SQL Server Books Online here:
http://msdn.microsoft.com/library/en-us/acdata/ac_8_con_7a_1hf7.asp

Take care not to over-use locking hints as they can hurt you more than help
you if you use them when you don't need to..

HTH

Regards,
Greg Linwood
SQL Server MVP

"YZ" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...





Similar Threads

1. Row-level locking & concurrent access

I have a VB6 rich client application that accesses SQL Server 2000
using ADO.  The database has a table of customers.  The application has
a form that shows customer details, and allows the user to navigate the
to next/previous customer in the table.

When user A is the first to open customer 1's details, I want them to
"take ownership" of that record, such that only user A can make changes
to the record.  When user B navigates to customer 1, I want user B to
be able to view the customer's details, but not be able to change them.

However, if user B navigates to customer 2, whose record is on the same
data page as customer 1, I do want user B to be able to take ownership
of customer 2's record, and be able to make changes.

In short, I want row-level locking on the customers table.  I was
relying on the client updating a customer record to indicate they have
ownership, but ADO caches changes for (I believe) 5 seconds, so if two
users view the same customer within five secs of each other, the second
user cannot ascertain that the first user has taken ownership.

How do I implement row-level locking? Or is there another way?

2. Deadlock when executing concurrent updates of one table - SQL Server

3. Concurrent select is locked while another transaction executes

4. Concurrent select is locked while another transaction executes Ins - SQL Server

5. Notification of row locking in case of concurrent updates

When concurrent updates happen to a row of a table in SQL Server 2000,
one user should be notified that row is already locked by some other
user. 

Can this be done? How?

6. Deadlocked on lock problem in sql with multithreading

7. Transactions, Locks and Deadlocks

Hi Chaps

Can someone point me in the right direction for a very in depth
practical treatment of this for sql2k.

Seen a number of books that skirt around it, read some articles that
touch on it (including some of erlands) but I need more...

Thanks for any tips!

8. Deadlock due to shared locks