views:

68

answers:

2

Hello everyone,

I am using SQL Server 2008 Enterprise. I am wondering whether this store procedure cause deadlock if executed by multiple threads at the same time? Another question is -- is it best practices we define begin and end transaction inside store procedure, or defining begin and end transaction from client code (e.g. ADO.Net code)?

create PROCEDURE [dbo].[FooProc]    
(  
 @Param1 int 
 ,@Param2 int  
 ,@Param3 int  
)    
AS    

DELETE FooTable WHERE  Param1 = @Param1     

INSERT INTO FooTable    
 (  
 Param1  
 ,Param2  
 ,Param3  
  )    
 VALUES    
 (  
 @Param1  
 ,@Param2  
 ,@Param3  
  )    

DECLARE @ID bigint    
 SET @ID = ISNULL(@@Identity,-1)    
 IF @ID > 0    
 BEGIN    
      SELECT IdentityStr FROM FooTable WHERE ID = @ID 
 END  

thanks in advance, George

+1  A: 

The code you have given could cause deadlock. Even if the stored procedure purely consisted of the following statement deadlock could occur.

   DELETE FooTable WHERE  Param1 = @Param1

Depending upon the exact table definition and indexes available (which you have left out of your question).

Martin Smith
Why consisting of this delete statement will cause deadlock? Any more details? I want to learn why it matters whether there is index on Param1 for deadlock?
George2
Well the way I was thinking was suppose that invocation 1 of the stored procedure ends up getting page locks on pages x,y,z but needs page a. Invocation 2 of the stored procedure ends up getting page locks on pages a,b,c but needs page x then deadlock would ensue.
Martin Smith
Can you show me sample in my context please? :-) e.g. in the delete statement, page x,y,z and a, b, c are for what page? Why a single delete statement will involve so many pages?
George2
@George2 We have no idea how many pages will be affected by it as you haven't told us!
Martin Smith
If only one record and Param1 is index, what will happen? And if more than one record will be impacted and Param1 is not indexed, what will happen? I do not want to solve this issue itself, but want to learn from you guru guys how to analyze this deadlock issue in different scenarios?
George2
I am most definitely not a deadlock guru! Check out Remus Rusanu's posts for that http://stackoverflow.com/search?q=user%3A105929+deadlock
Martin Smith
I did not find similar scenarios to my case, like why a single delete statement (as you quoted) will cause deadlock?
George2
single delete statement or single delete record? If the latter then I can't construct a scenario either where it would happen. If the former then the earlier example I gave would do it (maybe relying on a parallel execution plan just to ensure that the locks weren't issued in a linear order).
Martin Smith
Sorry Martin, the question is not very clear. Please discuss here, which I had a more clearer description.http://stackoverflow.com/questions/2981121/why-single-sql-delete-statement-will-cause-deadlock
George2
+2  A: 

The only reliable way to answer your question is to run your own stress tests

AlexKuznetsov
Sorry Alex, the question is not very clear. Please discuss here, which I had a more clearer description.http://stackoverflow.com/questions/2981121/why-single-sql-delete-statement-will-cause-deadlock
George2

related questions