Reputation: 469
I have one table with starting data and multiple consumer applications working on different machines in local network.
Please suggest how do I can orginize them to take each record from this database only once, so there will no be any situation where 2 or more apps taken the same record from the table.
I see 2 possible ways to resolve it, but not sure:
Please advise..
Upvotes: 3
Views: 712
Reputation: 27478
What you are asking for is default database behavior. You could use "SELECT ...... FOR UPDATE" and the database engine will lock the row and make it unavailable to other users. However this is only good practice if your program wants to hold the data for a short length of time (less than 1 second) your application which holds the data for much longer would encounter a lot of deadlock and contention problems.
You could do this in application code by having a couple of columns like "RESERVED_BY_USER" and "RESERVED_UNTIL", which the applications could manage themselves.
However from what you are describing I think what you really want is a queue as described here
Upvotes: 0
Reputation: 1844
Add one more field to the table with ProcessedBy and you can set the value to null if no machine is started processing it and set the value to the machinename if it started processing. While getting the record you can verify this processedBy field and if it is null you can assign to any machine.
Upvotes: 0
Reputation: 1062745
Various options:
NEWID
perhaps) to select rows that are unlikely to conflicthowever, personally there are other storage metaphors that work nicely here, for example "redis" has a few suitable operations:
all of these are atomic, avoiding all the bits that would make this a pain in SQL.
Upvotes: 1
Reputation: 50672
To retrieve the row, create a stored procedure that:
To update the row, create a stored procedure that:
Extra's:
Upvotes: 3