I have some ASP.NET C# code which executes a stored procedure in SQL Server via the SqlCommand and SqlConnection classes.
One of the stored procedures that gets executed is giving the error: "Transaction (Process ID 272) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction." This only happens occassionally.
Is there a way to get around this in my ASP.Net application? One thing I tried is ensuring that no 2 users entered the stored procedure concurrently:
object synclock =new object() ;lock (synclock) {// execute SQL stored procedure ...}This did not solve the problem, and I'm not even sure if that is the correct implementation to ensure sequential execution of the stored procedure.
I use a method which some frown upon but if you can handle dirty reads you will be fine. (sql 2000 SP4 or sql 2005) - use the no lock table hint on all your selects and joins in the procedure.
for example: select field1... from table1 with (NOLOCK) on joins its ... inner join table2 with (nolock) ...
So this will ask sql to read the data even if its in flux but if you don't care it will help with dead locks.
-c
No comments:
Post a Comment