 
     SELECT @now = SYSDATETIMEOFFSET()
 
+
+
+	BEGIN TRAN
+
+	SELECT TOP 1 @resourceName = ResourceName 
+
+	FROM Leases WITH (XLOCK,ROWLOCK)
+
+	WHERE 
+
+		Pool = @pool
+
+			AND 
+
+		(Expires IS NULL OR Expires < @now) 
+
+			AND 
+
+		DATEADD(MILLISECOND, @waitIntervalMilliseconds, LastReleased) < @now 
+
+		ORDER BY LastReleased;
+
     
 
+    UPDATE Leases
+
+        SET LastGranted = @now,
+
+            Expires = DATEADD(MILLISECOND, @leaseDurationMilliseconds, @now),
+
+            Token = @token
+
+		output inserted.[ResourceName], 
+
+		       inserted.[Pool],
+
+			   deleted.[LastGranted],
+
+			   inserted.[LastReleased],
+
+			   inserted.[Expires],
+
+			   inserted.[Token]
+
+        WHERE 
+
+            ResourceName = @resourceName
+
+			AND 
+
+			[Pool] = @pool
+
+
+
+    COMMIT TRAN
+
+
+
+    END
+
+
+
+
+
+GO
+
+
+
+IF object_id('[Alluvial].[ExtendLease]') IS NULL
+
+    exec('CREATE PROCEDURE [Alluvial].[ExtendLease] AS SELECT 1')
+
+GO
+
+
+
+ALTER PROCEDURE [Alluvial].[ExtendLease]
+
+    @resourceName nvarchar(100),
+
+    @byMilliseconds int, 
+
+    @token int  
+
+    AS
+
+    BEGIN
+
+
+
+    SET NOCOUNT ON;
+
+    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
+
+
+
     BEGIN TRAN
 
 
 
-    SELECT @resourceName = (SELECT TOP 1 ResourceName FROM Leases WITH (XLOCK,ROWLOCK)
+    DECLARE @expires datetimeoffset(7)
+
+
+
+    SELECT @expires = 
+
+    (SELECT Expires FROM Alluvial.Leases WITH (XLOCK,ROWLOCK)
 
         WHERE 
 
-            Pool = @pool
+            ResourceName = @resourceName 
 
                 AND 
 
-            (Expires IS NULL OR Expires < @now) 
+            Token = @token)
+
+
+
+    UPDATE Alluvial.Leases
+
+        SET Expires = DATEADD(MILLISECOND, @byMilliseconds, @expires)
+
+        WHERE 
+
+            ResourceName = @resourceName 
 
                 AND 
 
-            DATEADD(MILLISECOND, @waitIntervalMilliseconds, LastReleased) < @now 
-
-            ORDER BY LastReleased)
-
-
-
-    SELECT @lastGranted = LastGranted
-
-        FROM Leases 
+            Token = @token
+
+                AND 
+
+            Expires >= SYSDATETIMEOFFSET()
+
+
+
+    SELECT Expires FROM Alluvial.Leases
 
         WHERE 
 
-            ResourceName = @resourceName
+            ResourceName = @resourceName 
 
                 AND 
 
-            Pool = @pool
+            Token = @token
+
+
+
+    IF @@ROWCOUNT = 0
+
+        BEGIN
+
+            ROLLBACK TRAN;
+
+            THROW 50000, 'Lease could not be extended', 1;
+
+        END
+
+    ELSE
+
+        COMMIT TRAN;
+
+    END
+
+
+
+GO
+
+
+
+IF object_id('[Alluvial].[ReleaseLease]') IS NULL
+
+    exec('CREATE PROCEDURE [Alluvial].[ReleaseLease] AS SELECT 1')
+
+GO
+
+
+
+ALTER PROCEDURE [Alluvial].[ReleaseLease]
+
+    @resourceName nvarchar(100)  , 
+
+    @token int  
+
+    AS
+
+    BEGIN
+
+    SET NOCOUNT ON;
+
+
+
+    DECLARE @now DATETIMEOFFSET(7)
+
+    SELECT @now = SYSDATETIMEOFFSET()
 
 
 
     UPDATE Leases
 
-        SET LastGranted = @now,
-
-            Expires = DATEADD(MILLISECOND, @leaseDurationMilliseconds, @now),
-
-            Token = @token
-
-        WHERE 
-
-            ResourceName = @resourceName
-
-                AND 
-
-            Pool = @pool
-
-
-
-    COMMIT TRAN
-
-
-
-    SELECT [ResourceName],
-
-           [Pool],
-
-           [LastGranted] = @lastGranted,
-
-           [LastReleased],
-
-           [Expires],
-
-           [Token] 
-
-    FROM Leases 
+    SET LastReleased = @now,
+
+        Expires = null
 
     WHERE ResourceName = @resourceName 
 


        [Token] [int] NULL,

     CONSTRAINT [PK_Leases_1] PRIMARY KEY CLUSTERED 

    (



    DECLARE @now datetimeoffset

    DECLARE @token int


