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 :
Microsoft Dynamics AX (Archived)
Answered

Lock a table record.

(0) ShareShare
ReportReport
Posted on by

Hello,

I want to lock a table record.

When another trying to select that record, it wait, until I unlock the record.

How can I do that?

X++ (AX 2012).

Thanks :) 

*This post is locked for comments

I have the same question (0)
  • Ivan (Vanya) Kashperuk Profile Picture
    on at

    you select it for update using the standard X++ syntax

    To ensure it actually acquires a lock you need to use pessimistic concurrency

    however, it's discouraged in AX, as this is a multi-user environment, and locks cause large delays for other users wanting to update data in the same table

  • Community Member Profile Picture
    on at

    I do:

    Station 1:

    ...

    ttsbegin;

    myTable.selectForupdate();

    ...

    Station 2:

    ttsbegin;

    myTable.selectForupdate(); // same record.

    ... // there is no waiting !!!

    I need to do "wait" after myTable.selectForupdate for station 2.

    Thanks :)

  • Martin Dráb Profile Picture
    237,855 Most Valuable Professional on at

    First of all, selectForUpdate() just sets the update flag for the next select statement; it doesn't make any call to database to lock anything. Your snippet doesn't show whether you execute such select statements or not.

    Secondly, Ivan clearly wrote that you have to use pessimistic concurrency control, but you didn't mention anything about it. If the table should always use pessimistic concurrency, set its OccEnable property to No. If it should be for the particular selects only, use "select pessimisticLock" or set concurrencyModel() on the buffer.

  • Community Member Profile Picture
    on at

    OK, I will check that out.

    Thanks :)

  • Community Member Profile Picture
    on at

    Well,

    I did select for update.

    i.e:

    static void splTestRec1(Args _args)

    {

       GeneralJournalAccountEntry generalJournalAccountEntry;

       ;

       ttsBegin;

       select forupdate generalJournalAccountEntry

       where generalJournalAccountEntry.RecId == 12345;    

       ttsCommit; // *****

    }

    1. I Ran the above on one session, and the debugger command is before commit ran.

    2. I Ran the above on other session, and when doing the "select" statement, I accept that there will be "waiting", and there isn't any.

    How can I do lock+wait? Need a code example, please.

    Thanks :)

  • Verified answer
    Martin Dráb Profile Picture
    237,855 Most Valuable Professional on at

    Where did you set pessimistic locking, as mentioned in both Ivan's and my answers? Replace forUpdate with pessimisticLock and try it again.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans