locked
CRM 2011 -plugin problem with multiple users accessing it. RRS feed

  • General discussion

  • I have an autonumber logic in plugin that increments values and uses that no to tag each record.

    SO,

    Record 1 -1001

    Record 2 - 1002 and so on.

    SO, when multiple  user hit the plugin,how  will i ensure that duplicacy is not there(coz 1001,1002,.... are stored in db itself and pulled from the db tobe incremented to next no.)

    In short how u can ensure thread  safety here.

    Thursday, August 1, 2013 1:20 PM

All replies

  • Hi,

    You must create a transaction lock before you increment your entity counter field.

    There are various ways of doing this (External webservice, custom external database), but the best way is to use the Plugin Transaction it's self. This makes sure that two execution pipelines do not interfere with each other (even if on different servers in a load balanced scenario).

    You will need to create a dummy field on your counter entity and create a PreOperation plugin (this is in the db transaction)

    1. Update the counter entity's dummy attribute with a random guid - this will now create a read lock so other transactions will be blocked until this plugin has completed.

    2. Retrieve the counter value 

    3. Increment and store in the target entity of the current pipeline

    4. Update the counter entity with the incremented counter value

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

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, August 1, 2013 1:44 PM
    Answerer
  • Thanks Scott.

    can u plz explain the below step of urs,

    "Update the counter entity's dummy attribute with a random guid - this will now create a read lock so other transactions will be blocked until this plugin has completed."

    How a random guid will create a read lock so other transactions will be blocked until this plugin has completed


    And the url in "More info"  doesnt open up.
    • Edited by Kar Pallavi Thursday, August 1, 2013 3:02 PM
    Thursday, August 1, 2013 2:00 PM
  • Hi,

    I know it sounds odd - but actually by updating the dummy attribute value, the database takes an update lock on the record which means that no other transactions can proceed until the current one has completed. This allows you then to reliably read the most recent value knowing that no other plugin execution will get the same counter value.

    Try this url: http://bit.ly/1chu3uF

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, August 1, 2013 3:00 PM
    Answerer
  • Hi Scott,

    I went through original post for auto numbering provided by you.

    Its a great article.

    Just one more  query,

    can u plz share how to store the sequence values in web resource.Just a sample xml


    • Edited by Kar Pallavi Thursday, August 1, 2013 3:22 PM
    Thursday, August 1, 2013 3:20 PM