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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to produce Deadlock Exception in D365FO using X++

(4) ShareShare
ReportReport
Posted on by 2

Hi All,

I am working on application insights, and I have a log for exceptions generated, I want to check if deadlock exception will be shown in that log or not. in order to check this, first I have to generate deadlock exception.

Can anyone let me know how we can we produce the deadlock exception in D365FO using x++

Thanks In Advance…!

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    238,199 Most Valuable Professional on at
    I would do something like this:
     
    Have two separate DB connections. One will be the default one, the other created by UserConnection class. You'll update two tables with pessimistic locking, i.e. either the tables will have Ooc Enabled = No or you'll select records for update with pessimisticLock keyword.
     
    Let's say you have one record in TableA and one in table B. Update TableA record in connection 1. Update TableB record in connection 2. Then update the same TableB record in connection 1 and TableA record in connection 2. Each connection will have both updates in the same transaction.
     
    Now connection 1 is waiting for the lock on TableB held by connection 2. And connection 2 is waiting for the lock on TableA held by connection 1. That's a deadlock - if the DB server didn't detect this situation, they would stay blocked forever.
     
  • Suggested answer
    Sachin Mittal Profile Picture
    2 on at
    Thanks this Worked for me, adding code here two, what I did is created two runnable classes and run these using class runner, and deadlock exception is generated
    internal final class DeadLock_B
    {
      static  void execute1()
        {
            InventSum inventSum;
            CustTable custTable;
    
            ttsBegin;
            
            select forUpdate inventSum where inventSum.RecId == 68719638196;
    
            sleep(10000);
    
            select pessimisticlock custTable where custTable.RecId == 22565424065;
            custTable.selectForUpdate(true);
    
            ttsCommit;
           
        }
    
        /// <summary>
        /// Class entry point. The system will call this method when a designated menu
        /// is selected or when execution starts and this class is set as the startup class.
        /// </summary>
        /// <param name = "_args">The specified arguments.</param>
        public static void main(Args _args)
        {
            OA_DeadLock_B::execute1();
        }
    
    }
    
    
    internal final class DeadLock_A
    {
        static  void execute2()
        {
            InventSum inventSum;
            CustTable custTable;
    
            ttsBegin;
        
            select pessimisticlock custTable where custTable.RecId == 22565424065;
            custTable.selectForUpdate(true);
    
            sleep(10000); 
    
            // Attempt to access Resource X
            select forUpdate inventSum where inventSum.RecId == 68719638196; 
    
            ttsCommit; 
        }
    
        /// <summary>
        /// Class entry point. The system will call this method when a designated menu 
        /// is selected or when execution starts and this class is set as the startup class.
        /// </summary>
        /// <param name = "_args">The specified arguments.</param>
        public static void main(Args _args)
        { 
            OA_DeadLock_A::execute2();
        }
    
    }
    generated
  • Diana Sousa Profile Picture
    2 on at
    Hi, 
    I tried replicate the example from @Sachin Mittal but i don´t have the exception deadlock. 
    Why ? 
    I use two users different, and 2 browsers different.
    Really he has behavior correct, but does not show any error.

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

News and Announcements

Season of Giving Solutions is Here!

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 763 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 413 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 284 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans