AX 2012 locking concurrency – sample code

by | Updated August 15, 2016 | Development, Dynamics AX

AX 2012 locking concurrency

When we’re writing code that is updating or inserting records in the database, we need to be aware of AX 2012 locking concurrency and how the structure of our code could impact locking. It can be helpful to have some sample code and a SQL script to test out different scenarios.

AX 2012 locking concurrency – sample code

This job can be used to see the locking behavior in a really simple update, and it could be easily modified for other scenarios…

staticvoid LockExample(Args _args)

{

    CustTable ct;

    ttsbegin;

    WhileselectforUpdate ct where ct.AccountNum == “US-004”

    {

        ct.BankCentralBankPurposeText = “UpdatedText”;

        ct.update();

        print ct.BankCentralBankPurposeText;

    }

    ttscommit;

 }

Put a breakpoint on the While statement, on the Update, Print and on ttscommit.

Open SQL Server Management Studio, and run the following script at each breakpoint. Again, this script could be easily modified to include additional information from the SQL DMV’s. This is just an example.

select t1.request_session_id,t1.request_type, t1.request_status,t1.resource_type, db_name(es.database_id),es.login_time,es.program_name,es.login_name,es.nt_user_name, es.open_transaction_count from master.sys.dm_tran_locks t1
with (nolock) join master.sys.dm_exec_sessions es with (nolock) on t1.request_session_id = es.session_id where
es.program_name = ‘Microsoft Dynamics AX’
and es.login_name = ‘CORP\admin’
and es.open_transaction_count > 0

 

The locks are acquired on the Update statement but they are not released until the ttscommit. Because we specified forUpdate in our Select statement above, concurrency will default to that specified on the table. The table custTable has OCC Enabled set to Yes, so the Select uses Optimistic Concurreny. We can force the use of Pessimistic Concurrency by replacing the forUpdate like this…

While select
pessimisticLock ct where ct.AccountNum == “US-004”

Try changing the job to run this way and this time put a breakpoint right after the Select. Notice the difference in the locking behavior. Pessimistic Concurrency Control locks records as soon as they are fetched from the database for an update. AX 2012 locking concurrency

MSDN has more information on Optimistic Concurrency Control, a feature of AX 2012 locking concurrency, and discusses deciding which concurrency model to use.

http://msdn.microsoft.com/en-us/library/bb190073.aspx

 

Related Posts

  • **Updated 11/20/2013** There are quite a few important keyboard shortcuts that can really save some time when entering data and moving around in AX 2012.  One in particular - hitting…

  • The much anticipated release of AX 2012 R2 has been announced. Here are the new features that have my attention. I'll be sure to post some detail, as I explore…

  • The Layer concept is unique to Microsoft Dynamics AX Development and can cause some confusion for developers who are new to the AX development environment. In Dynamics AX we have 8 standard…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Upcoming Events

october

07oct12:00 pm1:00 pmThe Three Paths to Business Central from Dynamics GP

08oct11:00 am12:00 pmConfab with Stoneridge - Livestream - The Vision and Strategy of Microsoft Business Systems

14oct10:00 am10:30 amThe Modern Manufacturer - Managing Complex Cost Modeling

14oct12:00 pm12:30 pmGenerating Custom Inspection or Process Forms

19octAll Day22Stoneridge Connect Fall 2020

22oct11:00 am12:00 pmConfab with Stoneridge - Livestream - Stoneridge Connect Recap

28oct10:00 am10:30 amThe Modern Manufacturer - Engineering Change Management: Introduction of NEW Functionality for Manufacturers Using Dynamics 365

november

11nov10:00 am10:30 amThe Modern Manufacturer - Tears and Trauma of MRP

About Stoneridge
Stoneridge Software is a unique Microsoft Gold Partner, with emphasis on partner. With specialties in Microsoft Dynamics 365, Microsoft Dynamics AX, Microsoft Dynamics NAV, Microsoft Dynamics GP and Microsoft Dynamics CRM, we focus on attracting the most knowledgeable experts in the field to our team, and prioritize delivering stellar solutions with maximum impact for your business. At Stoneridge, we are deeply committed to your results. Each engagement is met with a dedicated team, ready to provide thorough, tailored, and expert service. Based in Minnesota, we intentionally “step into your shoes,” wherever you are. We focus on what you care about, and develop trusting, long-term relationships with our clients.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!

X