Skip to main content

Email notification on sqlserver database lock

We have a database lock situation happens on one production applications, it's caused by the application design, when one user make change and does not save it quickly, the other users will be blocked with no helpful information but hang screen.

I build this procedure to run every minute and send email alert to dba team and support staff, so we can talk to the blocker users to save the change or logoff for other users to continue their works.

This procedure only list the top 1 waiter and blocker, this is good enough for this application.

Here is the script.

CREATE  procedure [dbo].[alert_on_databaselock] as
declare @blockee_session_id varchar(100)
declare @blockee_wait_type varchar(100)
declare @wait_duration_ms varchar(100)
declare @blocker_session_id varchar(100)
declare @start_time varchar(100)
declare @status varchar(100)
declare @blockee_command varchar(100)
declare @database_id varchar(100)
declare @blockee_user_id varchar(100)
declare @blockee_host_name varchar(100)
declare @blockee_program_name varchar(100)
declare @blockee_login_name varchar(100)
declare @blockee_nt_user_name varchar(100)
declare @blockee_sql varchar(100)
declare @blocker_user_id varchar(100)
declare @blocker_host_name varchar(100)
declare @blocker_program_name varchar(100)
declare @blocker_login_name varchar(100)
declare @blocker_nt_user_name varchar(100)
declare @blocker_sql varchar(100)
declare @resource_description varchar(100)

SELECT top 1
@blockee_session_id=dm_ws.session_id ,
@blockee_wait_type = dm_ws.wait_type,
@wait_duration_ms=dm_ws.wait_duration_ms,
@blocker_session_id=dm_ws.blocking_session_id,
@resource_description=dm_ws.resource_description,
@start_time=dm_r.start_time,
@status=dm_r.status,
@blockee_command =dm_r.command,
@database_id=dm_r.database_id,
@blockee_user_id=dm_r.user_id ,
@blockee_host_name=dm_es.host_name ,
@blockee_program_name=dm_es.program_name,
@blockee_login_name=dm_es.login_name ,
@blockee_nt_user_name=dm_es.nt_user_name,
@blockee_sql=dm_t.text ,
@blocker_user_id=dm_r_blocker.user_id ,
@blocker_host_name=dm_es_blocker.host_name ,
@blocker_program_name=dm_es_blocker.program_name ,
@blocker_login_name=dm_es_blocker.login_name,
@blocker_nt_user_name=dm_es_blocker.nt_user_name,
@blocker_sql =dm_t_blocker.text
FROM sys.dm_os_waiting_tasks dm_ws
INNER JOIN sys.dm_exec_requests dm_r ON dm_ws.session_id = dm_r.session_id
INNER JOIN sys.dm_exec_requests dm_r_blocker ON dm_ws.blocking_session_id = dm_r_blocker.session_id
INNER JOIN sys.dm_exec_sessions dm_es ON dm_es.session_id = dm_r.session_id
left outer join sys.dm_exec_sessions dm_es_blocker ON dm_ws.blocking_session_id = dm_es_blocker.session_id
CROSS APPLY sys.dm_exec_sql_text (dm_r.sql_handle) dm_t
CROSS APPLY sys.dm_exec_sql_text (dm_r_blocker.sql_handle) dm_t_blocker
CROSS APPLY sys.dm_exec_query_plan (dm_r.plan_handle) dm_qp
WHERE dm_es.is_user_process = 1
and dm_ws.blocking_session_id is not null
order by dm_ws.wait_duration_ms desc

if @@rowcount=1
begin
declare @email_text varchar(1000)
declare @temptext varchar(1000)
set @email_text = 'Do not reply to this email, talk to DBA team about the database lock, ctvdba@ctv.ca'+char(10)+char(10)

set @temptext = '@blocker_host_name is : '+isnull(@blocker_host_name,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@blockee_host_name is : '+isnull(@blockee_host_name,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@Wait start time is : '+isnull(@start_time,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@Wait duration is (miliseconds) : '+isnull(@wait_duration_ms,'N/A')+char(10)
set @email_text = @email_text+@temptext


set @temptext = '@Wait Type is : '+isnull(@blockee_wait_type,'N/A')+char(10)
set @email_text = @email_text+@temptext


set @temptext = '@Blocker session id is : '+isnull(@blocker_session_id,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@The blockee session id is : '+isnull(@blockee_session_id,'N/A')+char(10)
set @email_text=@email_text + @temptext


set @temptext = '@Resource dscription is : '+isnull(@resource_description,'N/A')+char(10)
set @email_text = @email_text+@temptext


set @temptext = '@Wait status is : '+isnull(@status,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@Blockee command is : '+isnull(@blockee_command,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@Database name is : '+isnull(db_name(@database_id),'N/A')+char(10)
set @email_text = @email_text+@temptext


set @temptext = '@blockee_program_name is : '+isnull(@blockee_program_name,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@blockee_login_name is : '+isnull(@blockee_login_name,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@blockee_nt_user_name is : '+isnull(@blockee_nt_user_name,'N/A')+char(10)
set @email_text = @email_text+@temptext


set @temptext = '@blocker_program_name is : '+isnull(@blocker_program_name,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@blocker_login_name is : '+isnull(@blocker_login_name,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@blocker_nt_user_name is : '+isnull(@blocker_nt_user_name,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = '@blocker_sql is : '+isnull(@blocker_sql,'N/A')+char(10)
set @email_text = @email_text+@temptext

set @temptext = char(10)+char(10)+'@blockee_sql is : '+isnull(@blockee_sql,'N/A')+char(10)
set @email_text = @email_text+@temptext

/*
print 'debug 4'
print @email_text
*/
EXEC msdb.dbo.sp_send_dbmail @recipients='myemail@gmail.com',
    @subject = 'db block notification',
    @body = @email_text,
    @body_format = 'text' ;
end;
GO

exec alert_on_databaselock

Comments

Popular posts from this blog

non-existent process lock port on windows server

I have a database link created between oracle and sqlserver using oracle tg4odbc, the product is installed on windows server and run as service "OracleOraGtw11g_home1TNSListener", but sometime the service cannot started, the root cause of this problem is that the port number 1521 is used by an non-existent process. The first step is to use netstat -bano|find "1521" to get the process id, in my case it's 5844, which shows the connection is from my oracle server 10.8.0.169 H:\>netstat -bano|find "1521"   TCP    0.0.0.0:1521           0.0.0.0:0              LISTENING       5844   TCP    10.14.45.33:1521       10.8.0.169:42987       ESTABLISHED     5844 however the process id does not show in either task manager or process explorer. The next step is to run tcpview, which shows non-existent under process column, there are three rows, two show status as "listening", the other one shows status "established", right click and k

Opatch apply/lsinventory error: oneoff is corrupted or does not exist

I am applying the quarterly patch for 19c RDBMS, I tried using napply but failed, but somehow it corrupted the inventory though nothing applied. further apply and lsinventory command ran into error like this: $ ./OPatch/opatch lsinventory Oracle Interim Patch Installer version 12.2.0.1.21 Copyright (c) 2020, Oracle Corporation.  All rights reserved. Oracle Home       : /u02/app/oracle/19.0.0 Central Inventory : /u01/app/oraInventory    from           : /u02/app/oracle/19.0.0/oraInst.loc OPatch version    : 12.2.0.1.21 OUI version       : 12.2.0.7.0 Log file location : /u02/app/oracle/19.0.0/cfgtoollogs/opatch/opatch2020-09-08_13-35-59PM_1.log Lsinventory Output file location : /u02/app/oracle/19.0.0/cfgtoollogs/opatch/lsinv/lsinventory2020-09-08_13-35-59PM.txt -------------------------------------------------------------------------------- Inventory load failed... OPatch cannot load inventory for the given Oracle Home. LsInventorySession failed: Unable to create patchObject Possible ca

shell script to clean up oracle dumpfile

https://github.com/iacosta/personal/blob/master/shells/cleanhouse.sh #!/bin/ksh # # Script used to cleanup any Oracle environment. # # Cleans:      audit_file_dest #              background_dump_dest #              core_dump_dest #              user_dump_dest #              Clusterware logs # # Rotates:     Alert Logs #              Listener Logs # # Scheduling:  00 00 * * * /networkdrive/dba/scripts/purge/cleanup.sh -d 7 > /u01/dba/bin/cleanup.log 2>&1 # # Created By:  Lei Dao # # # RM="rm -f" RMDIR="rm -rf" LS="ls -l" MV="mv" TOUCH="touch" TESTTOUCH="echo touch" TESTMV="echo mv" TESTRM=$LS TESTRMDIR=$LS SUCCESS=0 FAILURE=1 TEST=0 HOSTNAME=`hostname` ORAENV="oraenv" TODAY=`date +%Y%m%d` ORIGPATH=/usr/local/bin:$PATH ORIGLD=$LD_LIBRARY_PATH export PATH=$ORIGPATH # Usage function. f_usage(){   echo "Usage: `basename $0` -d DAYS [-a DAYS] [-b DAYS] [