This table is used as a message queue with multiple readers and writers:
CREATE TABLE IF NOT EXISTS MyQueue(
id CHAR(36) PRIMARY KEY NOT NULL,
at DateTime NOT NULL,
message TEXT NOT NULL,
INDEX(at ASC));
I try to prevent double reading of the messages by using update locks:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT message FROM MyQueue WHERE at <= @today ORDER BY at ASC LIMIT 1 FOR UPDATE;
UPDATE MyQueue SET at=@nextDay WHERE at <= @today ORDER BY at ASC LIMIT 1;
(Idea is to remove message latter today after successful processing by one of the readers or reprocess tomorrow.)
But it looks like that some double reads are still possible. What to pay attention to?
atfield is not unique. 2) Perform this action as one query, not 2 separate ones.