Solved

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

Posted on 2014-04-09
3
292 Views
Last Modified: 2014-04-11
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.
0
Comment
Question by:GouthamAnand
3 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39989283
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
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 39989837
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
 

Author Closing Comment

by:GouthamAnand
ID: 39994446
Thanks for you both. I like both the answers.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now