LLI Wiki — My Digital Knowledge Base

Diagnose failed connections to Azure SQL and SQL Server

Two queries to list failed connections: sys.event_log on Azure SQL Database and the connectivity ring buffer.

Two queries list failed connections. The first reads the sys.event_log view of Azure SQL Database. The second reads the connectivity ring buffer (RING_BUFFER_CONNECTIVITY) and joins the text of the error messages.

Failed connections in the event log

SELECT *
FROM sys.event_log
WHERE event_type = 'connection_failed';

Connectivity errors in the ring buffer

;WITH RingBufferConnectivity AS
(
    SELECT
        records.record.value('(/Record/@id)[1]', 'int') AS [RecordID],
        records.record.value('(/Record/ConnectivityTraceRecord/RecordType)[1]', 'varchar(max)') AS [RecordType],
        records.record.value('(/Record/ConnectivityTraceRecord/RecordTime)[1]', 'datetime') AS [RecordTime],
        records.record.value('(/Record/ConnectivityTraceRecord/SniConsumerError)[1]', 'int') AS [Error],
        records.record.value('(/Record/ConnectivityTraceRecord/State)[1]', 'int') AS [State],
        records.record.value('(/Record/ConnectivityTraceRecord/Spid)[1]', 'int') AS [Spid],
        records.record.value('(/Record/ConnectivityTraceRecord/RemoteHost)[1]', 'varchar(max)') AS [RemoteHost],
        records.record.value('(/Record/ConnectivityTraceRecord/RemotePort)[1]', 'varchar(max)') AS [RemotePort],
        records.record.value('(/Record/ConnectivityTraceRecord/LocalHost)[1]', 'varchar(max)') AS [LocalHost]
    FROM
    (
        SELECT CAST(record AS xml) AS record_data
        FROM sys.dm_os_ring_buffers
        WHERE ring_buffer_type = 'RING_BUFFER_CONNECTIVITY'
    ) TabA
    CROSS APPLY record_data.nodes('//Record') AS records (record)
)
SELECT RBC.*, m.text
FROM RingBufferConnectivity RBC
    LEFT JOIN sys.messages M
        ON RBC.Error = M.message_id AND M.language_id = 1033
WHERE RBC.RecordType = 'Error' -- Comment out to see all RecordTypes
ORDER BY RBC.RecordTime DESC;

The result gives, among other columns, the time, the error code and message, the remote host and port, and the local host.

Mastodon LLIMastodon SocialFosstodon