1 Mar 2009 haruspex   » (Journeyer)

nconway, I don't see why people try to solve races like the Doctors problem through transactions and isolation levels. Row level locking in InnoDB prevents the race regardless of the isolation level in use.

BEGIN;
SELECT COUNT(*) INTO @n FROM Duties WHERE Shift=@s AND Status='on 
duty' 
FOR UPDATE;
if @n > 1 then
    UPDATE Duties SET Status='reserve' WHERE DoctorId=@d AND Shift=@s;
    COMMIT;
else
    ROLLBACK;

If you're not familiar with InnoDB, the SELECT establishes a critical section covering the on duty doctors. This same sequence executing concurrently will block until the connection with the lock has checked the count. The COMMIT (or ROLLBACK) will unblock the other connection waiting for the lock. In case the first connection committed an UPDATE which reduced the count to 1, the second connection, when unblocked, will see the new count of 1 and not make any update.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!