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
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. 🙂