-- Copyright (c) Microsoft Corporation. All rights reserved. -- -- PROCEDURE InsertInstanceState -- if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[InsertInstanceState]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[InsertInstanceState] GO Create Procedure [dbo].[InsertInstanceState] @uidInstanceID uniqueidentifier, @state image, @status int, @unlocked int, @blocked int, @info ntext, @ownerID uniqueidentifier = NULL, @ownedUntil datetime = NULL, @nextTimer datetime, @result int output, @currentOwnerID uniqueidentifier output As declare @localized_string_InsertInstanceState_Failed_Ownership nvarchar(256) set @localized_string_InsertInstanceState_Failed_Ownership = N'Instance ownership conflict' set @result = 0 set @currentOwnerID = @ownerID declare @now datetime set @now = GETUTCDATE() SET TRANSACTION ISOLATION LEVEL READ COMMITTED set nocount on IF @status=1 OR @status=3 BEGIN DELETE FROM [dbo].[InstanceState] WHERE uidInstanceID=@uidInstanceID AND ((ownerID = @ownerID AND ownedUntil>=@now) OR (ownerID IS NULL AND @ownerID IS NULL )) if ( @@ROWCOUNT = 0 ) begin set @currentOwnerID = NULL select @currentOwnerID=ownerID from [dbo].[InstanceState] Where uidInstanceID = @uidInstanceID if ( @currentOwnerID IS NOT NULL ) begin -- cannot delete the instance state because of an ownership conflict -- RAISERROR(@localized_string_InsertInstanceState_Failed_Ownership, 16, -1) set @result = -2 return end end else BEGIN DELETE FROM [dbo].[CompletedScope] WHERE uidInstanceID=@uidInstanceID end END ELSE BEGIN if not exists ( Select 1 from [dbo].[InstanceState] Where uidInstanceID = @uidInstanceID ) BEGIN --Insert Operation IF @unlocked = 0 begin Insert into [dbo].[InstanceState] Values(@uidInstanceID,@state,@status,@unlocked,@blocked,@info,@now,@ownerID,@ownedUntil,@nextTimer) end else begin Insert into [dbo].[InstanceState] Values(@uidInstanceID,@state,@status,@unlocked,@blocked,@info,@now,null,null,@nextTimer) end END ELSE BEGIN IF @unlocked = 0 begin Update [dbo].[InstanceState] Set state = @state, status = @status, unlocked = @unlocked, blocked = @blocked, info = @info, modified = @now, ownedUntil = @ownedUntil, nextTimer = @nextTimer Where uidInstanceID = @uidInstanceID AND ((ownerID = @ownerID AND ownedUntil>=@now) OR (ownerID IS NULL AND @ownerID IS NULL )) if ( @@ROWCOUNT = 0 ) BEGIN -- RAISERROR(@localized_string_InsertInstanceState_Failed_Ownership, 16, -1) select @currentOwnerID=ownerID from [dbo].[InstanceState] Where uidInstanceID = @uidInstanceID set @result = -2 return END end else begin Update [dbo].[InstanceState] Set state = @state, status = @status, unlocked = @unlocked, blocked = @blocked, info = @info, modified = @now, ownerID = NULL, ownedUntil = NULL, nextTimer = @nextTimer Where uidInstanceID = @uidInstanceID AND ((ownerID = @ownerID AND ownedUntil>=@now) OR (ownerID IS NULL AND @ownerID IS NULL )) if ( @@ROWCOUNT = 0 ) BEGIN -- RAISERROR(@localized_string_InsertInstanceState_Failed_Ownership, 16, -1) select @currentOwnerID=ownerID from [dbo].[InstanceState] Where uidInstanceID = @uidInstanceID set @result = -2 return END end END END RETURN Return Go GRANT EXECUTE ON [dbo].[InsertInstanceState] TO state_persistence_users GO -- -- PROCEDURE RetrieveAllInstanceDescriptions -- if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[RetrieveAllInstanceDescriptions]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[RetrieveAllInstanceDescriptions] GO Create Procedure [dbo].[RetrieveAllInstanceDescriptions] As SELECT uidInstanceID, status, blocked, info, nextTimer FROM [dbo].[InstanceState] GO -- -- PROCEDURE UnlockInstanceState -- if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UnlockInstanceState]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[UnlockInstanceState] GO Create Procedure [dbo].[UnlockInstanceState] @uidInstanceID uniqueidentifier, @ownerID uniqueidentifier = NULL As SET TRANSACTION ISOLATION LEVEL READ COMMITTED set nocount on Update [dbo].[InstanceState] Set ownerID = NULL, unlocked = 1, ownedUntil = NULL Where uidInstanceID = @uidInstanceID AND ((ownerID = @ownerID AND ownedUntil>=GETUTCDATE()) OR (ownerID IS NULL AND @ownerID IS NULL )) Go GRANT EXECUTE ON [dbo].[UnlockInstanceState] TO state_persistence_users GO -- -- PROCEDURE RetrieveInstanceState -- if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[RetrieveInstanceState]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[RetrieveInstanceState] GO Create Procedure [dbo].[RetrieveInstanceState] @uidInstanceID uniqueidentifier, @ownerID uniqueidentifier = NULL, @ownedUntil datetime = NULL, @result int output, @currentOwnerID uniqueidentifier output As Begin declare @localized_string_RetrieveInstanceState_Failed_Ownership nvarchar(256) set @localized_string_RetrieveInstanceState_Failed_Ownership = N'Instance ownership conflict' set @result = 0 set @currentOwnerID = @ownerID SET TRANSACTION ISOLATION LEVEL REPEATABLE READ BEGIN TRANSACTION -- Possible workflow status: 0 for executing; 1 for completed; 2 for suspended; 3 for terminated; 4 for invalid if @ownerID IS NOT NULL -- if id is null then just loading readonly state, so ignore the ownership check begin Update [dbo].[InstanceState] set ownerID = @ownerID, ownedUntil = @ownedUntil where uidInstanceID = @uidInstanceID AND ( ownerID = @ownerID OR ownerID IS NULL OR ownedUntil1 AND status<>3 AND status<>2 -- not blocked and not completed and not terminated and not suspended AND ( ownerID IS NULL OR ownedUntil 0 ) BEGIN -- lock the table entries that are returned Update [dbo].[InstanceState] set ownerID = @ownerID, ownedUntil = @ownedUntil WHERE blocked=0 AND status<>1 AND status<>3 AND status<>2 AND ( ownerID IS NULL OR ownedUntil1 AND status<>3 AND status<>2 -- not blocked and not completed and not terminated and not suspended AND ((unlocked=1 AND ownerID IS NULL) OR ownedUntil