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 2000 Forums
 SQL Server Development (2000)
 LOCKING

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-03-21 : 08:09:34
Vyas writes "Hi,

I have a stored procedure (SpGetFaxCaseForUser) which reads a table FaxBatchTable (Field = Status). This stored procedure is called from a VB application which is installed in several user work stations. Or you can say, this application continiuosly monitors the table FaxBatchTable for any rows with status = 1 and several instances of this application are active at the same time.

Stored Proc contains this statement:

select top 1 * from faxbatchtable where status = 1

if @@rowcount = 1
-- Process the row

The problem here is since multiple user workstations have this application installed and all of them monitors this table, sometimes, 2 application instances get the same row, which creates a lot of problems.

Can I block this in any way? I tried putting transaction lock level and it solved this problem. But it created other problems as this table is highly used by other applications also and it started giving DEADlock.

Is there any other solution?

Can I use rowlock?

select top 1 * from faxbatchtable with (rowlock) where status = 1
-- Will this solve my issue?
"

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2006-03-21 : 08:18:49
Are you sure that this is the statement that causes the deadlock? And is "status" indexed in any way in the faxbatchtable? How many instancs of your vb-app are active at the same time?

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-21 : 08:27:52
Refer this
http://sql-server-performance.com/deadlocks.asp

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-03-21 : 09:52:03
one solution could be to add a column to your faxbatchtable that would "reserve" the row for a given instance. So the code would change to something like:

set rowcount 1
update faxBatchTable set ReservedBy = <someValueUniqueToTheCallingInstance> where status = 1
set rowcount 0

--process the row where ReservedBy = <someValueUniqueToTheCallingInstance> and status = 1
then set the status = <processedStatus>

This just uses sql servers implicit transactions to insure only one row/user with no collisions and no blocking/deadlocking.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -