Connect-DbaInstance - Return the connection of a server object after reading its current database - #10728
Merged
potatoqualitee merged 1 commit intoSep 22, 2026
Conversation
…reading its current database (do Connect-DbaInstance) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
andreasjordan
marked this pull request as ready for review
September 20, 2026 19:46
potatoqualitee
approved these changes
Sep 21, 2026
potatoqualitee
left a comment
Member
There was a problem hiding this comment.
Reviewed the complete connection-lifecycle patch at this exact head, including SMO ownership semantics, pooled/multi-input/error cleanup paths, regression coverage, discussions, and the passing exact-head MULTI CI lane. No material defects found.
Member
|
going to get chatgpt pro on this, brb |
Member
|
awesome, pro approves 👏🏼 |
potatoqualitee
deleted the
connect-dbainstance-return-input-connection
branch
September 22, 2026 13:28
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.
Summary
Connect-DbaInstance -SqlInstance <server object> -Database Xhas to know which database the connection of that object is on, and a server object that was connected without-Databasehas an emptyCurrentDatabaseuntil its connection is opened once. The command opened it with an explicitConnectionContext.Connect()and left it open.That connection never went back to the pool: SMO returns a pooled connection after each batch only when it opened the connection itself, an explicit
Connect()keeps it checked out untilDisconnect(). So every server object that went through this path kept one session for the life of the process, which only aDisconnect-DbaInstanceon that very object could release.ClearAllPoolsdoes not touch a checked-out connection either.The visible consequence, found during the parallel full runs of 2026-09-19/20: a loop that hands fresh server objects to
Invoke-DbaQuery -SqlInstance $server -Database Xexhausts the 100-connection pool after 100 iterations and every later connect fails with "Timeout expired ... obtaining a connection from the pool". The test harness hit that after ~100 test files because its environment check does exactly this once per file.The fix reads the value and returns the connection right away, unless the caller had opened it before, in which case it stays theirs. SMO reconnects on the next batch as it always did. The value is kept in a variable, because
Disconnect()clearsCurrentDatabaseagain.The clone path itself (Copy, DatabaseName, the connection string variants) was checked step by step on the lab and is clean; the only session that stayed behind was the one of the object passed in.
Measurement
Sessions of the calling process on the instance, counted after
ClearAllPoolsso that only checked-out connections and the counting session itself remain (pwsh, SQL Server 2019):$server = Connect-DbaInstance(no -Database)Invoke-DbaQuery -SqlInstance $server -Database master -Query "SELECT 1"$server.ConnectionContext.SqlConnectionObject.State= Open$server.ConnectionContext.Disconnect()-Database master, then the same queryWindows PowerShell 5.1 behaves the same.
Test plan
Connect-DbaInstance.Tests.ps1: three server objects with distinct client names go throughConnect-DbaInstance -Database master(same database, no clone) and throughInvoke-DbaQuery -Database tempdb(clone), the session count afterClear-DbaConnectionPoolmust not grow.development("Expected 2, but got 5" and "Expected 2, but got 8"), the rest of the file passes.created by Claude and reviewed by Andreas Jordan
🤖 Generated with Claude Code