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

Announcements

No record found.

News and Announcements icon
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

  • Ivan (Vanya) Kashperuk Profile Picture
    Microsoft Employee 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
    241,501 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
    241,501 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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Prashant Singh Profile Picture

Prashant Singh 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans