Skip to content

Registered server commands disconnect the connection of the caller, including the Get- commands #10572

Description

@andreasjordan

Seven sites in the registered server commands disconnect a connection they did not open. They share one mechanism, so they need one fix rather than seven, and they were missed in the inventory in #10554.

What happens

Get-DbaRegServerStore builds the store on a connection from Connect-DbaInstance (Get-DbaRegServerStore.ps1:76), which hands back the server object of the caller when one was passed in. The commands then close that connection when they are done:

Site Statement
Get-DbaRegServerGroup.ps1:181 $serverstore.ServerConnection.Disconnect()
Get-DbaRegServer.ps1:222 $serverstore.ServerConnection.Disconnect()
Add-DbaRegServerGroup.ps1:155 $currentInstance.ConnectionContext.Disconnect()
Move-DbaRegServerGroup.ps1:133 $parentserver.ServerConnection.Disconnect()
Move-DbaRegServer.ps1:129 $parentserver.ServerConnection.Disconnect()
Remove-DbaRegServerGroup.ps1:118 $parentserver.ServerConnection.Disconnect()
Remove-DbaRegServer.ps1:134 Disconnect-RegServer -Server $regserver.Parent

The private helper Disconnect-RegServer walks up the parents until it finds a ServerConnection and disconnects that.

Steps to Reproduce

$server = Connect-DbaInstance -SqlInstance $instance -NonPooledConnection
$null = $server.ConnectionContext.ExecuteNonQuery("CREATE TABLE #marker (id INT)")

$null = Get-DbaRegServerGroup -SqlInstance $server -Id 1

$server.ConnectionContext.ExecuteScalar("SELECT COUNT(*) FROM #marker")

Measured on SQL Server 2019 with current development, a temp table as the marker and Get-DbaDatabase as a control:

Get-DbaRegServerGroup                      session survived: False
Get-DbaRegServer                           session survived: False
Add-DbaRegServerGroup                      session survived: False
Remove-DbaRegServerGroup                   session survived: False
Get-DbaDatabase (control)                  session survived: True

Two Get- commands are in that list. Reading registered servers ends the session of the connection that was handed in - temp tables, SET options, session context, all gone, and the connection is silently reopened afterwards so nothing looks wrong.

Why one fix and not seven

Removing the disconnect in Add-DbaRegServerGroup alone changes nothing, because it calls Get-DbaRegServerGroup, which disconnects as well. Anything short of a decision that covers the whole family leaves the symptom in place. That is what makes this different from the individual sites in #10554.

The decision is the same one as in #10564: only close what this module opened. Get-DbaRegServerStore is where the connection is obtained, so it is the natural place to know the answer - it can ask Connect-DbaInstance with -IsNewConnectionReference and pass that on, and Disconnect-RegServer becomes the single place that acts on it.

Test

Per tests/CLAUDE.md, one regression test per fixed command in the shape used for #10554: connect with -NonPooledConnection, create a temp table, call the command with that server object, and assert the temp table is still there afterwards.


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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions