locked
CRM2011: AutoNumber Causing Duplicates Since CRM Upgrade RRS feed

  • Question

  • We delivered a Plug-In that would give us the next numerical number within a sequence. The code is launched via a Plug-In watching for the Create event.

    To ensure the Plug-In didn't allocate the same number to more than one individual we protected the code handling the number retrieval by using the following:

    private static object _lockProcess = new object();
    lock (_lockProcess)
    {
        // Get next number code
    }

    The records the benefiting from having the next number assigned to them are created by workflow. So it's possible to have multiple records created at the same time... each requesting the next number in the sequence.

    This has worked fine for 18 months, but recently we noticed pairs of records being assigned the same number. We've come to the conclusion that the Workflow engine must have changed and it's using more than one process or thread to allocate work.

    When our workflow created 10 new client records at the same time we ended up with 5 pairs of duplicate numbers. So it seems fair to say that two threads or processes must be being used by the workflow engine now.

    Is there a way to get the LOCK mechanism to protect our code, or do we need to seek a SQL based solution to allocate sequential numbers for new records?

    Any advice gratefully received.

    Steve

    Friday, December 20, 2013 11:18 AM

All replies

  • Hello,

    I believe that you will have to go SQL way to protect uniqueness of sequential numbers.


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Friday, December 20, 2013 12:13 PM
    Moderator
  • Using locking in code is not reliable, especially as you don't have control over the instantiation of any plugin classes.

    My preference (with OnPremise) is to use a SQL based solution as you have full control over the locking. However, I believe it is possible to manage this with a plugin running within the originating transaction (i.e. stage = 20); the key part of the design is to have a spearate entity with one record that contains the next number in the sequence, and to access this within the plugin, thus locking it at the SQL level


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

    Friday, December 20, 2013 12:23 PM
    Moderator
  • Thanks for the advice. I suspected the SQL route was going to be the only viable choice.

    Steve

    Friday, December 20, 2013 12:45 PM
  • You might want to check out the ADX Productivity Pack autonumber solution.  It can be used by workflows or plugins and follows the implementation design David mentioned.
    Friday, December 20, 2013 1:00 PM