I've got an incoming message queue implemented in a SQL database (currently MS SQL Server), accessible via a web service. This web service runs a legacy protocol we have no control over, and must return either a 'no messages in queue' response, or both:
- Details of the oldest message in the queue
- The number of other messages in the queue
The algorithm used for this is currently very crude:
- Select all messages in queue
- Number of messages remaining = row count - 1
- Update oldest message to set collected flag
We are moving to a scenario where this may now be concurrently accessed, and have obviously run into concurrency problems where the same message gets picked up twice.
The issue we have is that we need a portable solution with minimal deployment requirements. Ideally that means no stored procedures, and no engine-specific locks.
Does anyone have any bright ideas?
Thanks!