locked
Thread safety in crm 2011 RRS feed

  • Question

  • How to make a plugin thread safety.?

    Suppose concurrent users are hitting the  CRM, and plugin is  in update message, how  to ensure that every hit  to  plugin is thread safe.

    Is there any mutex or lock concept to handle the concurrency issue.

    Any suggestion/solution is appreciated.



    • Edited by Kar Pallavi Thursday, July 25, 2013 3:08 PM
    Thursday, July 25, 2013 1:27 PM

All replies

  • there is nothing for thread out of the box. If you have  crm 2011 you can register you plugin within the transaction stage, that will avoid duplicates. if you are in 4, it gets  more complicated. I've used a mutex but didn't work. you can use msmq or create a separated database on the same  sql server as the crm sql database. than you can use stored procedures with transaction.

    Pre-Event

    Pre-operation

    20

    Stage in the pipeline for plug-ins that are to execute before the main system operation. Plug-ins registered in this stage are executed within the database transaction.

    Platform Core Operation

    MainOperation

    30

    In-transaction main operation of the system, such as create, update, delete, and so on. No custom plug-ins can be registered in this stage. For internal use only.

    Post-Event

    Post-operation

    40

    Stage in the pipeline for plug-ins which are to execute after the main operation. Plug-ins registered in this stage are executed within the database transaction.


    I Hope I could help. If I have answered please mark as 'Answer'. If was just helpful, please vote. Thanks and happy coding!


    • Edited by SH_2017 Friday, July 26, 2013 6:48 AM more
    • Marked as answer by Kar Pallavi Thursday, August 1, 2013 1:27 PM
    • Unmarked as answer by Kar Pallavi Thursday, August 1, 2013 2:35 PM
    Friday, July 26, 2013 6:43 AM
  • Thanks BrunoLucas for your reply.

    can u please through some light on mutex concept.

    Thursday, August 1, 2013 1:27 PM
  • Even if you tried a mutex, at most this could only work if you've only the one CRM server. Also, you get no control over how the plugins are loaded by CRM, or the threading behaviour, so I wouldn't consider using any threading control.

    What is the concurrency problem that you're trying to resolve ?


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, August 1, 2013 3:49 PM
    Moderator
  • I  need to update entity A record from a plugin.

    And requirment is as below,

    Need to retieve some  counter value from an entityB  and based on these counter need to update entity A and then increment the conter value on B.

    In case of concurrent user using there is a  chance of duplicacy.so how to handle that.

    Friday, August 2, 2013 10:13 AM
  • You have to leverage the transactional behavior of the plugin event pipeline as mentioned before. Here's one approach. http://connect.greenbeacon.com/2012/01/technical-post-implementing-robust-microsoft-dynamics-crm-2011-auto-numbering-using-transactions/
    Friday, August 2, 2013 6:14 PM
  • Hi Kar Pallavi,

    You should go with inbuilt transaction support for Plugin. Please don't use Mutex and Lock objects,  They are not efficient to handle the duplication and causes the deadlock situation. I have used The Inbuilt Transaction support before and it guarantees for lock.

    Please refer this blog. Read all the comments carefully.

    http://www.crmsoftwareblog.com/2012/01/implementing-robust-microsoft-dynamics-crm-2011-auto-numbering-using-transactions/

    Thanks!

    Sunday, August 4, 2013 6:54 AM