Tri-State Locking in Dynamics 365 Business Central

Lock icon

Overview

The tri-state locking feature in Dynamics 365 Business Central is designed to improve the performance and concurrency of database transactions. By enabling this feature, AL-based read operations that follow write operations are performed optimistically. This increases concurrency and reduces blocked or failed operations.

Note: Using the LockTable method explicitly will disable optimistic reads.

Locking Behavior

When a session starts modifying data in a table, the Business Central server uses database locks. The locking behavior differs between versions 22 (and earlier) and the tri-state locking mechanism.

Versions 22 and Earlier

  • Example Code:
trigger OnAction()
var
currency1: Record Currency;
currency2: Record Currency;
begin
currency1.FindFirst(); // Uses READUNCOMMITTED

currency1.Code := 'DKK';
currency1."ISO Code" := 'DKK';
currency1.Symbol := 'Kr';
currency1.Insert();

currency2.FindLast(); // Uses UPDLOCK (exclusive lock)
end;
  • Behavior: Update locks are acquired, causing potential blocking issues until changes are committed or rolled back.

Tri-State Locking

  • Example Code:
trigger OnAction()
var
currency1: Record Currency;
currency2: Record Currency;
begin
currency1.FindFirst(); // Uses READUNCOMMITTED

currency1.Code := 'DKK';
currency1."ISO Code" := 'DKK';
currency1.Symbol := 'Kr';
currency1.Insert();

currency2.FindLast(); // Uses READCOMMITTED (shared lock)
end;
  • Behavior: Shared locks are acquired, allowing other sessions to concurrently read and write to the table if there are no conflicts.

Summary of Differences

Next Steps

Tri-state locking is enabled by default and can be managed via the Feature Management page in Business Central. For on-premises setups, the server configuration must include EnableTriStateLocking=true.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *