Ich habe eine Version von DeleteExpiredSessions (von Microsoft bereitgestellt), die Cursor verwendet und Daten in temporäre Tabellen speichert. You can find it hereASP.NET-Sitzungsverwaltung DeleteExpiredSessions Sieht albern aus?
Und dies ist der Quellcode der gespeicherten Prozedur
CREATE PROCEDURE [dbo].[DeleteExpiredSessions]
AS
SET NOCOUNT ON
SET DEADLOCK_PRIORITY LOW
DECLARE @now datetime
SET @now = GETUTCDATE()
CREATE TABLE #tblExpiredSessions
(
SessionID nvarchar(88) NOT NULL PRIMARY KEY
)
INSERT #tblExpiredSessions (SessionID)
SELECT SessionID
FROM dbo.ASPStateTempSessions WITH (READUNCOMMITTED)
WHERE Expires < @now
IF @@ROWCOUNT <> 0
BEGIN
DECLARE ExpiredSessionCursor CURSOR LOCAL FORWARD_ONLY READ_ONLY
FOR SELECT SessionID FROM #tblExpiredSessions
DECLARE @SessionID nvarchar(88)
OPEN ExpiredSessionCursor
FETCH NEXT FROM ExpiredSessionCursor INTO @SessionID
WHILE @@FETCH_STATUS = 0
BEGIN
DELETE FROM dbo.ASPStateTempSessions WHERE SessionID = @SessionID AND Expires < @now
FETCH NEXT FROM ExpiredSessionCursor INTO @SessionID
END
CLOSE ExpiredSessionCursor
DEALLOCATE ExpiredSessionCursor
END
DROP TABLE #tblExpiredSessions
RETURN 0
Ich frage mich, warum es nicht einfach ist abgelaufen Sitzungen mit folgenden Abfrage löschen:
DELETE [ASPState].dbo.ASPStateTempSessions
WHERE Expires < GETUTCDATE()
Warum ist das zu breit?, Sollte es einen Grund geben, warum sie diesen Weg implementiert haben oder es falsch ist. – FLICKER