• In our Production server, we have created a SQL Server Agent job which runs every 1 minute and monitors if there is any blocking for more than 10 seconds. In case it finds a blocking process, it send an email to us (DBAs) and we look into the issue in more details.

    Below is a sample code for this job

    SELECT * (here please include the information you want)

    FROM sys.dm_exec_requests DR

    INNER JOIN sys.dm_exec_sessions DS ON DR.Session_id = DS.Session_id

    CROSS APPLY sys.dm_exec_sql_text(dr.sql_handle) AS s2

    WHERE DR.Session_ID in

    (SELECT blocking_session_id

    FROM sys.dm_exec_requests

    WHERE (wait_time/10000) > @maxtime)

    Also, if you want to dig more deep like the table name, index name, type of lock etc, you may use the below query to get lock info

    SELECT request_session_id as spid,

    db_name(resource_database_id) as dbname,

    CASE

    WHEN resource_type = 'OBJECT' THEN

    object_name(resource_associated_entity_id)

    WHEN resource_associated_entity_id = 0 THEN 'n/a'

    ELSE object_name(p.object_id)

    END as entity_name, p.index_id,i.name IndexName,

    resource_type as resource,

    resource_description as description,

    request_mode as mode, request_status as status

    FROM sys.dm_tran_locks t LEFT JOIN sys.partitions p

    ON p.hobt_id = t.resource_associated_entity_id

    left join sys.indexes i on p.object_id = i.object_id and p.index_id = i.index_id

    WHERE resource_database_id = db_id();