How to process records which are not locked in master table.

Hi,
Here is a scenario to which I need good solution.

I keep getting messages into a table ABC (24 hours & 7 days a week). Every message contains unique ID and that goes into ID column of ABC table.

This same ID is also in another master table XYZ.

Now I have a Win service which processes the messages (24 hours & 7 days a week).

Win service selects top 1 unprocessed ID from ABC table and call the SP p_proc1 to process the message. p_procc1 takes ID as argument. Once the messge is processed it will mark the rcord as processed (sets the column 'processed' to 'Y')

Now the issue is - SP p_proc1 cannot process the messages if the record with same ID in XYZ table has been locked by some application. (Some UI application locks the records indefinitely for hours and sometimes even days.)

So while getting the ID from ABC table – I want to make sure it is NOT locked (rowlock) in XYZ table. How to do this?

Another question is - how to process continuously the messages in ABC table in database itself without Win Service? Can anyone suggest me the solution how to process the messages without Win service (I mean in database itself) 24/7 ?

If the record is locked in master table XYZ, it should process the message later when it is unlocked.- We are not sure when the record will be unlocked in XYZ table. Sometimes it may take 10 days to get unlocked.

I will be thanful if someone suggests me a good a solution for this.
GouthamAnandAsked:
Who is Participating?
 
Scott PletcherSenior DBACommented:
For performance reasons, I'd create a filtered index on the table.  

CREATE UNIQUE INDEX ABC__IX_Not_Processed ON dbo.ABC ( id )
    INCLUDE ( processed, locked )
    WHERE processed <> 'Y' AND locked <> 'Y'
    WITH ( FILLFACTOR = 99, ONLINE = ON, SORT_IN_TEMPDB = ON ) --change as needed
    ON [PRIMARY] --change as needed
   

Then to process:

DECLARE @id int
WHILE 1 = 1
BEGIN
    SELECT TOP (1) @id = id
    FROM dbo.ABC
    WHERE processed <> 'Y' AND locked <> 'Y' --<<-- exactly same conditions as in "create index"
    IF @@ERROR > 0 OR @@ROWCOUNT = 0
        BREAK
    EXEC dbo.p_proc1 @id
END --WHILE



CREATE UNIQUE INDEX ABC__IX_Not_Processed ON dbo.ABC ( id ) INCLUDE ( processed, locked )
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I don't know how you could get a records that is "not locked", unless this is defined in some column (like LOCKED='Y') status ...
if it's locked like you said using that other table, continue reading

so, for the processing part, you could create a sql agent job run do the same as the winservice:
declare @id int
select top 1 @id = id from ABC where not exists(select null from xyz where xyz.id = abc.id ) and ( processed is null or processed <> 'Y' )
while @@rowcount <> 0
begin
  exec yourproc @id

  select top 1 @id = id from ABC where not exists(select null from xyz where xyz.id = abc.id ) and ( processed is null or processed <> 'Y' )

end

Open in new window


hope this helps
0
 
GouthamAnandAuthor Commented:
Thanks for you both. I like both the answers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.