web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Calling the SDK with a different transaction within a plugin

(0) ShareShare
ReportReport
Posted on by 30

Hi,

 

We have an existing 'AutoNumbering' plugin running on create entity X that needs to increment a counter that is stored in entity Y and add the content to a field in entity X

The plugin is using a critical section (2 to 7 below) in order to avoid race conditions.

 

1 enter plugin for entity x

2 enter critical section (Mutex)

     3 retrieve the counter from y

     4 increment counter

     5 update counter in y

     6 set entity x's field = counter

7 Exit critical section (Mutex)

8 leave the plugin

 

The scenario above does not work correctly when concurrent clients call Create x. We sometimes get duplicate values in entity X

We kind of figured out that the update (5) does not execute until later when the threads leaves the plugin (8), but this is too late because a context switch already occurred and the next client is reusing the value counter that has not been saved yet.

 

Is there a way to call (3) and (5) using the SDK with a different transaction?

Thanks

Christian

I have the same question (0)
  • Suggested answer
    MehranBre Profile Picture
    732 on at

    hi,

    You can greatly optimize this process by adding a plugin on Retrieve Multiple Entity Y.

    To call the first record in plugin X from entity Y (of course with a special field in Column Set that is checked in plugin Y so that this plugin does not always run and only run if it is from this source)

    In the plugin on Y, which is written on the Post Operation stage, you add the record that was called to the count field and save the result in Y, and return the same result to plugin X.

    In this way, you only use this returned field in Plugin X, and creating multiple records at the same time does not disrupt this process.

    good luck.

  • Christian Betrisey Profile Picture
    30 on at

    Hi Mehran,

    Thanks for your answer. I think that I understand the idea. This is pretty smart.

    I will try to prototype it and update this post when I have a solution.

    Thanks again

    Christian

  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at

    Hello,

    Try using the lock statement in your plugin code, this way, if subsequent calls arrive and the code is already executing, they will be queued until the lock is released. This is a link on how the lock statement works: docs.microsoft.com/.../lock-statement

  • Christian Betrisey Profile Picture
    30 on at

    Hi Charles,

    Yes, I am using a lock between 2) and 7)  I called it 'Mutex', which is the same.

    The problem is that the save in 5) is not executed right away, but only when the plugin commit the transaction and exits, so the subsequent call will read the same value of the counter in 3).

    thanks

    Christian

  • Charles Abi Khirs Profile Picture
    3,569 on at

    Did you register the plugin on preoperation or postoperation?

  • Christian Betrisey Profile Picture
    30 on at

    Pre-validation since we want to update the autoNumber field in the entity when creating.

  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at

    Are you working with d365 on premise or online? If it is on-premise, is it a NLB environement or not? Bcz if it is not a NLB environement the lock should work well if it is implemented

  • Christian Betrisey Profile Picture
    30 on at

    We are running onPremise without LB.

    The lock works perfectly well. I can see that with the debugger. The problem is that within a plugin, there is only one transaction and it is committed at the end of the plugin.

    i.e. the call to

       service.Update(y); (at line 5 in the pseudo code)

    is not executed right away, but when the DB transaction commits at the end of the plugin i.e. after the locked section has terminated at line 8)

  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at

    Plz register your plugin in preoperation and not on prevalidation bcz the preoperation is inside the transaction and try.

  • Christian Betrisey Profile Picture
    30 on at

    I will try that and update the post with the results. Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans