Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Unblocking stored procedure (template)

$
0
0
[i]The other day I was asked to build a SQL Server process to terminate blocking sessions that could be safely destroyed in order not to drain necessary instance resources. The solution is made of below stored procedure and by a SQL Server Agent jobs that runs every 3 minutes just to invoke the sproc.The T-SQL code should be easy to read and has plenty of remarks. Any doubts please post here for extra info.[/i][code="sql"]USE [<<yourDBAgoodStuffDatabase...>>]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO[font="Courier New"]---- ===============================================-- Author: Paulo A. Nascimento ©-- Create date: 8th May 2015-- Description: A blocking sessions terminator usp -- ===============================================--[/font]CREATE PROCEDURE [dbo].[usp_Unblocker]ASBEGIN DECLARE @kMinWaitDuration smallint = 30000; -- 30 seconds, change as necessary SET NOCOUNT ON; IF ( ( SELECT COUNT(*) FROM sys.dm_os_waiting_tasks as A JOIN master.dbo.sysprocesses as B ON A.blocking_session_id = B.spid WHERE ( blocking_session_id IS NOT NULL ) AND (wait_duration_ms > @kMinWaitDuration) -- only blockings whose duration is bigger than N seconds... AND hostname like 'AnnoyingServer101' OR ( blocking_session_id IS NOT NULL AND (wait_duration_ms > @kMinWaitDuration) AND LEFT(hostname, 3) LIKE 'LAP' ) -- no laptops allowed to block our beloved SQL Server... business internal nomenclature for notebooks, adapt as needed OR ( blocking_session_id IS NOT NULL AND (wait_duration_ms > @kMinWaitDuration) AND LEFT(hostname, 3) LIKE 'WKS' ) -- no workstations allowed to block our beloved SQL Server... business internal nomenclature for client PCs, adapt as needed -- -- ... -- ADD/REMOVE business rules according the context of your organization! -- ) > 0 ) -- Is there blocking happening right now ? Yes, so let's kick ***... BEGIN SELECT TOP(1) @blocking_session_id_ = blocking_session_id, @wait_duration_ms_ = wait_duration_ms, @session_id_ = session_id, @wait_type_ = wait_type, @lastwaittype_= B.lastwaittype, @DBname_ = DB_NAME(B.dbid), -- as DBname @login_time_ = B.login_time, @last_batch_ = B.last_batch, @open_tran_ = B.open_tran, @status_ = B.status, @hostname_ = B.hostname, @program_name_ = B.program_name, @cmd_ = B.cmd, @loginName_ = B.loginame, @sqlHandle_ = B.sql_handle, @dateTS_ = getdate() FROM sys.dm_os_waiting_tasks as A JOIN master.dbo.sysprocesses as B ON A.blocking_session_id = B.spid -- We want to keep an historic of offending sessions and originators INSERT INTO [dbo].[blockingTable] VALUES ( @blocking_session_id_, @wait_duration_ms_, @session_id_, @wait_type_, @lastwaittype_, @DBname_, @login_time_, @last_batch_, @open_tran_, @status_, @hostname_, @program_name_, @cmd_, @loginName_, @sqlHandle_, @dateTS_ ) TRUNCATE TABLE #blockingTableAux] -- We clear temporary/auxiliary table (clone of blockingTable table so it keeps track of only current SPIDs... INSERT INTO [dbo].[blockingTableAux] VALUES -- Holding the session details for post-processing... ( @blocking_session_id_, @wait_duration_ms_, @session_id_, @wait_type_, @lastwaittype_, @DBname_, @login_time_, @last_batch_, @open_tran_, @status_, @hostname_, @program_name_, @cmd_, @loginName_, @sqlHandle_, @dateTS_ ) -- Start of demolition loop DECLARE @umSPID int = 0; DECLARE @SQLstatement nvarchar(10); -- should be enough for a string like 'kill 65535'... DECLARE @contador smallint = ( SELECT COUNT(*) FROM #blockingTableAux ); WHILE ( @contador > 0 ) -- while temporary table blockingTableAux is not empty... BEGIN SET @umSPID = (SELECT TOP (1) dbo.blockingTableAux.blockingSession_id FROM dbo.blockingTableAux); SET @SQLstatement = 'KILL ' + CAST( @umSPID as nvarchar(5) ); EXECUTE(@SQLstatement) -- executes the dynamic SQL generated in the line above... DELETE FROM #blockingTableAux WHERE #blockingTableAux.blockingSession_id = @umSPID SET @contador = @contador - 1; END ENDEND[/code]

Viewing all articles
Browse latest Browse all 3145

Trending Articles