Skip to content

Install-DbaMaintenanceSolution - Leave the connection of the caller alone - #10570

Merged
potatoqualitee merged 1 commit into
developmentfrom
fix-install-maintenancesolution-ownership
Aug 13, 2026
Merged

Install-DbaMaintenanceSolution - Leave the connection of the caller alone#10570
potatoqualitee merged 1 commit into
developmentfrom
fix-install-maintenancesolution-ownership

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

Fourth of the sites listed in #10554.

The disconnect

The command closed its connection when it was done, without asking whether it had opened it. When a caller passes their own server object, Connect-DbaInstance hands the same object back, and the command then closed the connection of the caller, taking the session with it. It now only closes what it opened itself.

The leak the disconnect was hiding

With -ReplaceExisting, the existing objects are dropped through $db.Invoke($cleanupQuery), which runs on the connection of the caller and leaves it in that database - #10555. The disconnect covered that up, because the connection was closed right afterwards.

The installation of the scripts a few lines below already names the database:

$null = Invoke-DbaQuery -SqlInstance $server -Database $Database -Query $sql -EnableException

so the cleanup now does the same. Same statements, same database, and the connection of the caller is untouched.

Tests

A new context installs with -ReplaceExisting through a connection the caller opened, and asserts that the session survives and that the connection is still on master. Each assertion covers one of the two changes, verified by putting each one back:

=== branch as committed (both fixes) ===
session survived: True
DB_NAME()       : master

=== with $db.Invoke() restored ===
session survived: True
DB_NAME()       : tempdb

=== with the disconnect guard removed ===
session survived: False
DB_NAME()       : master

Jobs are deliberately left out of that context (-InstallJobs not set), because the SQL Agent commands move the connection themselves - New-DbaAgentJob and Remove-DbaAgentJob are their own sites of #10555 and are not touched here.

Install-DbaMaintenanceSolution passes 52 of 52 against SQL Server 2025 and 2022, in 20 minutes.


This text was created by Claude and reviewed by Andreas Jordan.

…lone

The command closed its connection when it was done, no matter whether it had
opened it or whether the caller handed one over. Connect-DbaInstance now
reports that through -IsNewConnectionReference.

The disconnect was also hiding that the cleanup of the existing objects ran
through $db.Invoke(), which leaves the connection in that database. The
installation of the scripts a few lines below already names the database
with Invoke-DbaQuery, so the cleanup now does the same.

See #10554 and #10555

(do Install-DbaMaintenanceSolution)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@potatoqualitee
potatoqualitee merged commit 25578bb into development Aug 13, 2026
30 of 32 checks passed
@potatoqualitee
potatoqualitee deleted the fix-install-maintenancesolution-ownership branch August 13, 2026 18:36
@potatoqualitee

Copy link
Copy Markdown
Member

thank you 🙏🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants