IsloationLevel -Maximize Performance??

IsloationLevel -Maximize Performance??

In earlier versions of Microsoft Dynamics Business Central (BC), we often used methods like

Rec.LockTable(True), 
Rec.Findset(True), 
Rec.Findfirst()
Commit();        

  1. Rec.LockTable - it locks the record from the entire table
  2. Rec. Finset(True) - it locks the specific set of record

These methods frequently caused performance issues and bugs, especially with rollbacks. BC 23 introduces a new syntax:

Rec.ReadIsolation := IsolationLevel::<enum>,         

which enhances performance and transaction control.

What are Isolation Levels?

Isolation levels help manage how data is read and locked during transactions. BC 23 provides several levels:

  • ReadCommitted: Reads only committed data.
  • ReadUncommitted: Reads data, including uncommitted changes.
  • RepeatableRead: Maintains stable reads by holding locks.
  • UpdLock: Locks data for updates, preventing others from reading with the same intent.

Examples

Counting Committed Records

Use ReadCommitted to count only committed records:

local procedure GetCommittedRecordCount(): Integer
var
    GLEntry: Record "G/L Entry";
begin
    GLEntry.ReadIsolation := IsolationLevel::ReadCommitted;
    exit(GLEntry.Count());
end;        

Counting Uncommitted Records

Use ReadUncommitted to include uncommitted changes in the count:

local procedure GetEstimatedCount(TableNo: Integer): Integer
var
    RRef: RecordRef;
begin
    RRef.Open(TableNo);
    RRef.ReadIsolation := IsolationLevel::ReadUncommitted;
    exit(RRef.Count());
end;        

Getting the Next Entry Number

Use UpdLock to safely get the next entry number:

  1. IsolationLevel::UpdLock - The beauty of UpdLock and find Last will Lock only the last record from the table.

local procedure GetNextEntryNo(): Integer
var
    GLEntry: Record "G/L Entry";
begin
    GLEntry.ReadIsolation := IsolationLevel::UpdLock;
    GLEntry.FindLast();
    exit(GLEntry."Entry No." + 1);
end;        

By using these new isolation levels in BC 23, you can improve performance and ensure more reliable transaction handling in your Business Central applications.


要查看或添加评论,请登录

社区洞察

其他会员也浏览了