Transaction and error handling in BC AL - new knowledge articles#11
Open
Drakonian wants to merge 1 commit intomicrosoft:mainfrom
Open
Transaction and error handling in BC AL - new knowledge articles#11Drakonian wants to merge 1 commit intomicrosoft:mainfrom
Drakonian wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Author
|
@JesperSchulz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Transaction and error handling in BC AL - new knowledge articles
In my experience, many Business Central developers, including experienced ones, struggle with AL's transaction and error-handling model. It differs enough from SQL transaction patterns and from general try/catch semantics in other languages that reasoning about it without BC-specific guidance leads to real bugs: Commits inside loops, database writes wrapped in
[TryFunction]expecting rollback, event publishers unprotected from subscriber Commits, tests that die at the first Commit without any business-logic verdict.I tried to consolidate what I know about this area into a small, coherent set of articles so that a review agent citing any one of them naturally surfaces the rules that go with it.
What I added
Performance:
understand-implicit-transaction-boundary.md- the foundational article: AL manages write transactions automatically;Commit()splits an execution, it does not start one.codeunit-run-as-atomic-sub-operation.md(+ good/bad samples) -if Codeunit.Run(X) thenis the AL-idiomatic rollback boundary; a direct contrast with SQLBEGIN TRANhabits.codeunit-run-requires-prior-commit-inside-transaction.md(+ good/bad samples) - the BC rule thatCodeunit.Runcannot nest inside an open write transaction, and the loop trap that forces readers into per-row Commits.use-tryfunction-for-error-catching-not-rollback.md(+ good/bad samples) - the biggest single misconception:[TryFunction]catches errors but does not roll back database writes. Also covers the return-capture rule and the sharedGetLastErrorTextbuffer.Security:
commitbehavior-attribute-scopes-explicit-commits.md(+ good/bad samples) - the[CommitBehavior(::Ignore)]pattern for protecting an atomic publisher from third-party subscriber Commits.Testing (new domain
/microsoft/knowledge/testing/):transactionmodel-attribute-governs-test-transactions.md(+ good/bad samples) - matching[TransactionModel]to the commit behavior of the code under test; the first article in a new domain for test-infrastructure concerns.What I changed
avoid-commit-inside-loops.md- enriched with BC context (the runtime's implicit end-of-execution commit, theCodeunit.Run-based checkpoint alternative) and now ships agood.alsample demonstrating the chunked-checkpoint pattern. Both the good and bad samples were rewritten on a neutral operation (Customer.Namenormalization), replacing the earlier unrealistic directSales Headerstatus assignment.How the articles reference each other
Seven interlinked articles that a review agent can walk:
avoid-commit-inside-loops.md→understand-implicit-transaction-boundary.md,codeunit-run-as-atomic-sub-operation.mdunderstand-implicit-transaction-boundary.md→avoid-commit-inside-loops.md,codeunit-run-as-atomic-sub-operation.mdcodeunit-run-as-atomic-sub-operation.md→codeunit-run-requires-prior-commit-inside-transaction.md,use-tryfunction-for-error-catching-not-rollback.mdcodeunit-run-requires-prior-commit-inside-transaction.md→codeunit-run-as-atomic-sub-operation.md,avoid-commit-inside-loops.md,use-tryfunction-for-error-catching-not-rollback.mdcommitbehavior-attribute-scopes-explicit-commits.md→codeunit-run-requires-prior-commit-inside-transaction.md,use-tryfunction-for-error-catching-not-rollback.mduse-tryfunction-for-error-catching-not-rollback.md→codeunit-run-as-atomic-sub-operation.mdtransactionmodel-attribute-governs-test-transactions.md- standalone; the test-infrastructure concern doesn't intersect with the production-runtime articles above.Each article stays narrow enough to be cited alone. A finding about
[TryFunction]wrapping writes cites just the TryFunction article; a finding that touches atomic sub-operations and the prior-commit rule cites both and lets the reader walk the graph for the rest.Scope and what I left out
I kept scope to the core primitives -
Commit,Codeunit.Run,[TryFunction],[CommitBehavior],[TransactionModel]- and the specific interactions between them that developers demonstrably get wrong. A few things I considered but left out:Database.IsInWriteTransaction(). Documented but too thin to carry its own article. I added a short note in the prior-commit article's Anti Pattern section, framing it as a diagnostic tool and a code smell when used for production branching.DisableWriteInsideTryFunctions. Noted inline in the TryFunction article; not expanded, since the behavior is invisible in SaaS.Samples use
ClearLastError()immediately before anyGetLastErrorText()read, matching the Microsoft documentation idiom. Codeunit IDs are in the 50128–50156 range; none collide with existing samples.P.S. I use MS Learn documentation to validate every article, so this is why I point it to microsoft folder, let me know if I need to change it to community