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:
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.
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-DbaRegServerStorebuilds the store on a connection fromConnect-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: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:134Disconnect-RegServer -Server $regserver.ParentThe private helper
Disconnect-RegServerwalks up the parents until it finds aServerConnectionand disconnects that.Steps to Reproduce
Measured on SQL Server 2019 with current
development, a temp table as the marker andGet-DbaDatabaseas a control:Two
Get-commands are in that list. Reading registered servers ends the session of the connection that was handed in - temp tables,SEToptions, 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-DbaRegServerGroupalone changes nothing, because it callsGet-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-DbaRegServerStoreis where the connection is obtained, so it is the natural place to know the answer - it can askConnect-DbaInstancewith-IsNewConnectionReferenceand pass that on, andDisconnect-RegServerbecomes 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.