Avoiding Deadlock in Plugins

26 Nov


Multiple operations with the same Entity inside a Plugin, causes Deadlocks in Plugins and might cancel the transaction. Let’s explore the Trick to avoid the Deadlocks.


In one of my projects, there was a situation, when multiple operations had been done on the same Entity records. A Plugin was there, which was simultaneously updating a record lets say “C” and then from record “C” based on some condition record “B” was being updated and another Plugin was there which was updating values from record “B” to record “A”. And after it is done the final amount had to be shown in record “A” immediately as soon as the custom Popup (a Web Resource), that causes the operation in record “C” is closed and the Popup was being invoked from the ribbon of entity “A”.
As the user wanted to see the effect immediately there was no other option than to just make the Plugin Synchrounous. So, Create/Update and Retrieve operation at the same time was causing the transaction to be Deadlocked sometimes and was not allowing the user to Add/Update multiple records.


In order to solve the issue, I tried several ways. Finally I found that in QueryExpression there is a property called “NoLock” while using RetrieveMultiple method.

Its simple to use but its effect is something worthy to be noted. The code snippet is,

QueryExpression queryCItems= new QueryExpression("new_entityc");
queryCItems.NoLock = true;
queryCItems.ColumnSet = new ColumnSet("new_totalamount");
queryCItems.Criteria.AddCondition("new_aid", ConditionOperator.Equal, entC.Id);

This solved the issue. 🙂

By Dibyasingh Tripathy
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

Leave a comment

Posted by on November 26, 2013 in CRM Plug-in


Tags: , , , , , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: