FUN! Deadlock art generator
Autor: Luciano Moreira | Publicado: 2016-06-24T09:17:00-03:00

Contexto: Estou ministrando um treinamento in-company e preparando uma série de novas demonstrações para este cliente. Após escrever uma procedure que era para ser problemática, esbarrei em deadlocks mais interessantes do que eu havia planejado….

Gostei da brincadeira e resolvei compartilhar com vocês um deadlock art generator!

Algumas imagens da saída estão abaixo. Também coloquei no OneDrive um trace com alguns deadlocks que eu coletei.











A primeira ver que ouvi falar sobre deadlock art foi em um post do Catae (https://blogs.msdn.microsoft.com/fcatae/2010/03/18/deadlock-art/) que sempre cito no módulo de locking nos meus treinamentos.

Para reproduzir em seu ambiente, você vai fazer o seguinte.

&nbsp; &nbsp; 1. Crie a procedure proc_Divertida no AdventureWorks2014
&nbsp; &nbsp; 2. Utilizando o SQLQueryStress, mande 20/30 threads com 3/4 execuções “EXEC proc_Divertida;”
&nbsp; &nbsp; &nbsp; &nbsp; a. O número de threads/repetições você muda de acordo com seu interesse na arte. kkkk
&nbsp; &nbsp; 3. Monitore o deadlock graph no profiler

HAVE FUN!

Abraços,


USE AdventureWorks2014
GO

IF OBJECT_ID('dbo.proc_Divertida') IS NOT NULL
&nbsp; &nbsp; &nbsp;DROP PROCEDURE dbo.proc_Divertida
go

CREATE PROCEDURE dbo.proc_Divertida
AS&nbsp;

&nbsp; &nbsp; &nbsp;DECLARE @OrderID INT = (CAST((RAND() * 100000) AS INT) % 30000) + 43659;
&nbsp; &nbsp; &nbsp;PRINT @OrderID

&nbsp; &nbsp; &nbsp;BEGIN TRANSACTION

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UPDATE Sales.SalesOrderDetail
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SET UnitPrice = UnitPrice
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE SalesOrderID = @OrderID

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; WITH C AS (
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SELECT (Quantity * ActualCost) AS LineTotal, PRODUCTID, TransactionDate
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM Production.TransactionHistory
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UNION
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SELECT (Quantity * ActualCost), PRODUCTID, TransactionDate
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM Production.TransactionHistoryArchive
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UNION
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SELECT (UnitPrice * OrderQty), SOD.ProductID, SOH.OrderDate&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM Sales.SalesOrderDetail AS SOD
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN Sales.SalesOrderHeader AS SOH
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON SOH.SalesOrderID = SOD.SalesOrderID
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SELECT&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C.ProductID,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; YEAR(TransactionDate),
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SUM(LineTotal) &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM C
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GROUP BY GROUPING SETS (ProductID, YEAR(TransactionDate), (ProductID, YEAR(TransactionDate)), ())

&nbsp; &nbsp; &nbsp;COMMIT TRANSACTION
GO

Luciano Caixeta Moreira - {Luti}
luciano.moreira@srnimbus.com.br
www.twitter.com/luticm
www.srnimbus.com.br