Concurency issues with scheduling app

Posted by Sazug on Stack Overflow See other posts from Stack Overflow or by Sazug
Published on 2010-03-08T13:28:19Z Indexed on 2010/03/08 13:51 UTC
Read the original article Hit count: 269

Our application needs a simple scheduling mechanism - we can schedule only one visit per room for the same time interval (but one visit can be using one or more rooms). Using SQL Server 2005, sample procedure could look like this:

CREATE PROCEDURE CreateVisit
    @start datetime, @end datetime, @roomID int
AS
BEGIN
DECLARE @isFreeRoom INT

BEGIN TRANSACTION

SELECT @isFreeRoom = COUNT(*) 
FROM visits V
INNER JOIN visits_rooms VR on VR.VisitID = V.ID
WHERE @start = start AND @end = [end] AND VR.RoomID = @roomID

IF (@isFreeRoom = 0)
BEGIN
    INSERT INTO visits (start, [end]) VALUES (@start, @end)
    INSERT INTO visits_rooms (visitID, roomID) VALUES (SCOPE_IDENTITY(), @roomID)
END

COMMIT TRANSACTION
END

In order to not have the same room scheduled for two visits at the same time, how should we handle this problem in procedure? Should we use SERIALIZABLE transaction isolation level or maybe use table hints (locks)? Which one is better?

© Stack Overflow or respective owner

Related posts about sql-server-2005

Related posts about concurrency