我正在調用一個存儲過程,該存儲過程根據表中存在的過程密鑰執行INSERT
或UPDATE
。如何防止SQL Server存儲過程中出現死鎖?
到目前爲止程序正常工作。直到我們的用戶基礎開始增長。今天,我得到了以下錯誤這是通過重新啓動應用程序池運行的服務解決:
InsertDDM_UserDashboard error: RequestError: Transaction (Process ID 64) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
你怎麼能防止在SQL Server存儲過程的僵局?
我回顧了this link這表明它可能是一個問題,SELECT
和UPDATE
同時運行導致死鎖。但我的程序將這些陳述與IF..ELSE
條件分開,因此兩者不能同時運行。
存儲過程:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertDDM_UserDashboard]
@p_email VARCHAR(255),
@p_dashboardPreferences VARCHAR(4000),
@p_userDefaultDashboard VARCHAR(500)
AS
IF (NOT EXISTS(SELECT * FROM [dbo].[DDM_UserProfile] WHERE Email = @p_email))
BEGIN
INSERT INTO [dbo].[DDM_UserProfile]
([Email]
,[DashboardPreferences]
,DefaultDashboard
)
VALUES
(@p_email
,@p_dashboardPreferences
,@p_userDefaultDashboard
)
END ELSE BEGIN
UPDATE [dbo].[DDM_UserProfile]
SET [DashboardPreferences][email protected]_dashboardPreferences
WHERE [Email][email protected]_email
UPDATE [dbo].[DDM_UserProfile]
SET [email protected]_userDefaultDashboard
WHERE [Email][email protected]_email
END