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

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 ...

oracle dba_hist_sysmetric_summary

found this blog is helpful to get CPU and IO statistics on oracle database. http://shob-dbadmin.blogspot.ca/2012/12/how-to-find-total-io-of-database.html courtesy to  Shomil Bansal , below are hist writing, not mine. How to find total IO of the database instance Total IO of database instance is sum of the physical reads, physical writes and redo writes. There are several views to find these values. v$sysmetric  - Reports metric values for only the most current time sample 60 secs. v$sysmetric_summary  - Reports metric values for time sample of 1 hour. v$sysmetric_history  - Reports metric values every 60 sec from the time instance is up. Better way to analyse IO using this view to take deltas between two time periods. dba_hist_sysmetric_history  - All the above views are refreshed when the instance is restarted. This view, part of AWR, stores the historical stats. I have used this view for my report. Query: ====== set lines 350...

ORA_RMAN_SGA_TARGET

assume that we lost all the files of oracle database but we do have rman backup, when trying to bring up a dummy database before restore start, I get this error. RMAN> startup nomount force; WARNING: cannot translate ORA_RMAN_SGA_TARGET value startup failed: ORA-01078: failure in processing system parameters ORA-01565: error in identifying file '+DATA/PROD/spfilePROD.ora' ORA-17503: ksfdopn:2 Failed to open file +DATA/PROD/spfilePROD.ora ORA-15056: additional error message ORA-17503: ksfdopn:DGOpenFile05 Failed to open file +DATA/prod/spfileprod.ora ORA-17503: ksfdopn:2 Failed to open file +DATA/prod/spfileprod.ora ORA-15173: entry 'spfileprod.ora' does not exist in directory 'prod' ORA-06512: at line 4 starting Oracle instance without parameter file for retrival of spfile RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =================================...