Skip to content

Restart container before restoring demo data snapshot to free DB connections#8931

Open
aholstrup1 wants to merge 1 commit into
mainfrom
aholstrup1-laughing-eureka
Open

Restart container before restoring demo data snapshot to free DB connections#8931
aholstrup1 wants to merge 1 commit into
mainfrom
aholstrup1-laughing-eureka

Conversation

@aholstrup1

@aholstrup1 aholstrup1 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

What & why

CI runs that hit a transient deadlock during demo data generation were failing permanently instead of recovering. The demo-data retry logic (added in #8848) snapshots the tenant DB and restores it before re-attempting generation, but the restore itself died with:

Microsoft.Data.SqlClient.SqlError: Exclusive access could not be obtained because the database is in use.

Root cause: a failed generation attempt leaves a deadlocked/rolling-back NAV session whose SQL connection does not drain in time. Stop-NAVServerInstance returns before that connection is gone, so Restore-SqlDatabase ... -ReplaceDatabase can't get exclusive access to default. Because the deadlock reproduces consistently in some builds, the broken recovery path turned a recoverable retry into a guaranteed failure on every rerun.

This change restarts the container at the start of Restore-TenantDatabaseForDemoDataRetry. The restart forcibly terminates the SQL Server process (dropping every connection to default); NAV comes back idle, so the subsequent stop drains cleanly and the restore obtains exclusive access. The restore is still required afterward to discard the partial demo data the failed attempt left behind; the restart only clears connections, not data.

This only affects the recovery path, so the extra restart time is paid only when a generation attempt has already failed. The happy path is unchanged.

Linked work

AB#640951

How I validated this

  • I read the full diff and it contains only changes I intended.
  • I built the affected app(s) locally with no new analyzer warnings.
  • I ran the change in Business Central and confirmed it behaves as expected.
  • I added or updated tests for the new behavior, or explained below why none are needed.

What I tested and the outcome

Build-script (PowerShell) change only, no AL app changes. Verified the script parses with no errors. The fix targets the CI demo-data import path; validation is via the affected pipeline reaching the restore-and-retry path successfully. No automated tests exist for this container/SQL orchestration script.

Risk & compatibility

Low risk and confined to the demo-data retry recovery path used during CI test-data import. Adds a container restart (a few minutes) only when a generation attempt fails. The retry snapshot .bak lives inside the container filesystem, which a restart (not a recreate) preserves, so the subsequent restore still finds it.

…ections

The demo-data retry restore failed with 'Exclusive access could not be obtained because the database is in use' because Stop-NAVServerInstance returns before the deadlocked NAV session's SQL connection drains. Restart the container first so the restore can obtain exclusive access on 'default'.
@aholstrup1 aholstrup1 requested review from a team June 30, 2026 13:32
@github-actions github-actions Bot added the Build: scripts & configs Build scripts and configuration files label Jun 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Copilot PR Review

Iteration 1 · Outcome: not-applicable

The PR diff contains only a PowerShell file (build/scripts/ImportTestDataInBcContainer.ps1). All sub-skills declare technologies: [al] (or [al, javascript]) and returned not-applicable because no AL or JavaScript source changes are present in the diff.

Knowledge source: https://github.com/microsoft/BCQuality@822cae1b2771ac25f665f73369f69093bd4fd630

No findings were posted for this iteration.

Orchestrator pre-filter (13 file(s) excluded)

  • layer-disabled (knowledge) : 13 file(s)

Findings produced by the Copilot CLI agent against BCQuality at 822cae1b2771ac25f665f73369f69093bd4fd630. Reply 👎 on any inline comment to flag false positives.

@github-actions github-actions Bot modified the milestone: Version 29.0 Jun 30, 2026
@aholstrup1 aholstrup1 enabled auto-merge June 30, 2026 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Build: scripts & configs Build scripts and configuration files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant