Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-04-09
3
Medium Priority
?
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

597 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