SQLServer如何監控阻塞會話

Eric zhou發表於2024-05-28

一、查詢阻塞和被阻塞的會話

SELECT 
    r.session_id AS [Blocked Session ID],
    r.blocking_session_id AS [Blocking Session ID],
    r.wait_type,
    r.wait_time,
    r.wait_resource,
    s1.program_name AS [Blocked Program Name],
    s1.login_name AS [Blocked Login],
    s2.program_name AS [Blocking Program Name],
    s2.login_name AS [Blocking Login],
    r.text AS [SQL Text]
FROM sys.dm_exec_requests AS r
LEFT JOIN sys.dm_exec_sessions AS s1 ON r.session_id = s1.session_id
LEFT JOIN sys.dm_exec_sessions AS s2 ON r.blocking_session_id = s2.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS r
WHERE r.blocking_session_id <> 0;

二、找出阻塞的具體SQL

SELECT 
    r.session_id,
    r.blocking_session_id,
    t.text AS [SQL Text],
    r.wait_type,
    r.wait_time,
    r.wait_resource
FROM sys.dm_exec_requests AS r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
WHERE r.blocking_session_id <> 0;

三、編寫C#程式,每隔10秒監控SQL Server資料庫中的阻塞會話,定位出阻塞的根源會話並終止它們,同時記錄日誌。

using System;
using System.Data.SqlClient;
using System.IO;
using System.Timers;

class Program
{
    private static Timer timer;
    private static string connectionString = "your_connection_string_here";

    static void Main(string[] args)
    {
        timer = new Timer(10000); // 每10秒執行一次
        timer.Elapsed += CheckForBlockingSessions;
        timer.AutoReset = true;
        timer.Enabled = true;

        Console.WriteLine("Press [Enter] to exit the program.");
        Console.ReadLine();
    }

    private static void CheckForBlockingSessions(object source, ElapsedEventArgs e)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                string query = @"
                SELECT 
                    r.session_id AS BlockedSessionID,
                    r.blocking_session_id AS BlockingSessionID,
                    r.text AS SqlText
                FROM sys.dm_exec_requests AS r
                CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS r
                WHERE r.blocking_session_id <> 0;";

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            int blockedSessionId = reader.GetInt32(0);
                            int blockingSessionId = reader.GetInt32(1);
                            string sqlText = reader.GetString(2);

                            LogBlockingSession(blockedSessionId, blockingSessionId, sqlText);
                            KillSession(blockingSessionId);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            LogError(ex.Message);
        }
    }

    private static void KillSession(int sessionId)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string killQuery = $"KILL {sessionId};";
                using (SqlCommand killCommand = new SqlCommand(killQuery, connection))
                {
                    killCommand.ExecuteNonQuery();
                    LogKillSession(sessionId);
                }
            }
        }
        catch (Exception ex)
        {
            LogError($"Failed to kill session {sessionId}: {ex.Message}");
        }
    }

    private static void LogBlockingSession(int blockedSessionId, int blockingSessionId, string sqlText)
    {
        string logMessage = $"[{DateTime.Now}] Blocked Session ID: {blockedSessionId}, Blocking Session ID: {blockingSessionId}, SQL Text: {sqlText}";
        File.AppendAllText("blocking_sessions.log", logMessage + Environment.NewLine);
        Console.WriteLine(logMessage);
    }

    private static void LogKillSession(int sessionId)
    {
        string logMessage = $"[{DateTime.Now}] Killed Session ID: {sessionId}";
        File.AppendAllText("killed_sessions.log", logMessage + Environment.NewLine);
        Console.WriteLine(logMessage);
    }

    private static void LogError(string message)
    {
        string logMessage = $"[{DateTime.Now}] Error: {message}";
        File.AppendAllText("errors.log", logMessage + Environment.NewLine);
        Console.WriteLine(logMessage);
    }
}

說明

  1. 連線字串:替換 your_connection_string_here 為實際的資料庫連線字串。
  2. 定時器:使用 System.Timers.Timer 類設定每10秒執行一次檢查。
  3. 檢查阻塞會話:在 CheckForBlockingSessions 方法中,查詢阻塞會話和根源會話的資訊。
  4. 終止會話:在 KillSession 方法中,執行 KILL 命令來終止阻塞會話。
  5. 日誌記錄:日誌記錄包括阻塞會話的詳細資訊和終止會話的操作,以及錯誤資訊。

注意事項

  • 執行此程式需要確保有足夠的許可權來訪問資料庫和執行 KILL 命令。
  • 請仔細測試程式以確保其符合預期行為,尤其是在生產環境中。
  • 日誌檔案的路徑和許可權需要根據實際情況進行配置。

周國慶

2024/5/28

相關文章