locked
Pre Entity Images and SQL Server application locks RRS feed

  • Question

  • I noticed that if I have a plugin running on an entity and I run two updates on the same instance of the entity the updates will run in parallel if there are no pre entity images, but if there are pre entity images they will run in sequence. Ie. if I have a plugin that takes 10 seconds to run:

    Pre entity image registered:

    T) A presses save

    T + 1) B presses save

    T + 10) A's update finishes

    T + 20) B's update finishes with the pre entity image containing values populated after A's update finished

    Pre entity image not registered:

    T) A presses save

    T + 1) B presses save

    T + 10) A's update finishes

    T + 11) B's update finishes

    Looking at the sys.dm_tran_locks management view in SQL server I can see that an application lock is taken when the pre entity image is registered but no application lock is taken otherwise. Is this behavior documented anywhere? I have a situation where it would be very useful for me to be able to guarantee that updates on the same instance of an entity are always run in sequence rather than in parallel.

    Monday, July 7, 2014 10:53 AM

Answers

  • I'm pretty sure this is not documented anywhere. Microsoft are understandably reluctant to document the detail of the SQL-level implementation, as they will like the flexibility to change this.

    Rather than relying on the internal behaviour of CRM, I'd suggest you look at using SharedVariables as a means of coordinating plugins, as this gives you a means to exchange data between plugin instances


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

    Monday, July 7, 2014 3:37 PM
    Moderator

All replies

  • I'm pretty sure this is not documented anywhere. Microsoft are understandably reluctant to document the detail of the SQL-level implementation, as they will like the flexibility to change this.

    Rather than relying on the internal behaviour of CRM, I'd suggest you look at using SharedVariables as a means of coordinating plugins, as this gives you a means to exchange data between plugin instances


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

    Monday, July 7, 2014 3:37 PM
    Moderator
  • Thanks, I wasn't expecting it to be documented behavior.

    I don't think SharedVariables will help me. What I need is to be able to guarantee that only one update is running on one entity instance at a time. A simple example would be a plugin that increments a counter. If the plugin simply reads the existing counter value, increments it and writes it again, then I'm not guaranteed that the counter will reflect the number of updates - two updates could both read 1 and both write 2. It looks like I actually have that guarantee if I register a pre entity image but I don't want to rely on undocumented behavior. However, I found this blog which has a simpler trick: http://www.crmsoftwareblog.com/2012/01/implementing-robust-microsoft-dynamics-crm-2011-auto-numbering-using-transactions/ . Add a dummy field to the entity and write a random value to that as the first thing in the plugin. As the database has the read commited isolation level, only one transaction at a time will be able to get past that write and updates will always happen in sequence.

    Wednesday, July 9, 2014 5:22 AM