Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi,

I am facing following error "Transaction (Process ID 133) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction."

Scenario :
I am using "parallel for each" to do "bulk insert during same time some other thread tries to read data from the same table and i get dead lock error.

Please let me know how to solve it.
Posted

Hi, one of the possible variants is to execute each action in Transaction scope, for example:

C#
Parallel.ForEach(MyIEnumerableSource, insertAction);

static void insertAction(EntityItem item)
{
using (TransactionScope scope = new TransactionScope(
     Required, new TransactionOptions() {IsolationLevel = ReadCommitted))
  {   
try{
      ExecuteInsertQuery(item);
    scope.Complete ();
}
catch{scope.Rollback();}
}
 
Share this answer
 
Comments
[no name] 24-Dec-12 6:56am    
Hi Oleksandr thanks for your reply, I am getting error when another thread tries to read same table but some other data of the table.
Oleksandr Kulchytskyi 24-Dec-12 7:07am    
Sorry, obviously i inattentively read your question...
[no name] 24-Dec-12 7:16am    
hi,do you know how to solve this issue.
hi,

a) Select statement in sql server locks the table to avoid deadlock issue use with(nolock) in sql statement.

b) If no index is present on table then delete statement locks the whole table to avoid this create an index.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900