Invoke-DbaQuery - Test the database the connection is on, not the one it was opened with - #10563
Closed
andreasjordan wants to merge 1 commit into
Closed
Invoke-DbaQuery - Test the database the connection is on, not the one it was opened with#10563andreasjordan wants to merge 1 commit into
andreasjordan wants to merge 1 commit into
Conversation
… it was opened with
To decide whether the connection that was passed in can be reused, the
command compared ConnectionContext.DatabaseName with -Database. That
property only holds the database the connection was opened with. As soon as
anything runs a USE on that connection, the two differ, the connection is
reused although it is on another database now, and the query silently runs
in the wrong database:
$server = Connect-DbaInstance -SqlInstance $instance -Database tempdb -NonPooledConnection
$null = $server.ConnectionContext.ExecuteNonQuery("USE [master]")
Invoke-DbaQuery -SqlInstance $server -Database tempdb -Query "SELECT DB_NAME()"
# master
ConnectionContext.CurrentDatabase is the database the connection is on right
now, and it is also what Connect-DbaInstance compares, so both commands now
ask the same question. That also saves a needless trip through
Connect-DbaInstance when the connection is already on the wanted database.
See #10554
(do Invoke-DbaQuery)
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Collaborator
Author
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.
Draft: needs more testing, and waits for #10559.
Why this waits
The change sends the connection-reuse case through
Connect-DbaInstancein situations where it did not go there before. Ondevelopmentalone that would be unsafe, because the old disconnect logic then closes the connection of the caller - which is what #10559 fixes. So this is based on the branch of #10559 and only the second commit belongs here. GitHub retargets the pull request todevelopmentonce #10559 is merged.What is wrong
To decide whether the connection that was passed in can be reused, the command compares
ConnectionContext.DatabaseNamewith-Database. That property only holds the database the connection was opened with.Connect-DbaInstanceasks the same question withConnectionContext.CurrentDatabase, the database the connection is on right now.The two differ as soon as anything runs a
USEon the connection - which is exactly what the database-scoped SMO calls in #10555 do. The connection is then reused although it sits on another database, and the query silently runs in the wrong one:Measured effect
Using
@@SPIDto see whether a connection was reused, on SQL Server 2022:-Database <the database it is on>beforeConnect-DbaInstanceCurrentDatabaseis empty while the connection is closedTests
Two assertions: the connection is still reused while it is on the requested database, and a query runs in the requested database after the connection was moved away from it. Reverting the one-line change makes the second one fail with
Expected: 'tempdb' But was: 'master', so it does test the change. With the change,Invoke-DbaQuerypasses 31 of 31.What still needs testing before this leaves draft
Invoke-DbaQuerycarries a large part of the module and the reuse path is used everywhere-ReadOnlySqlConnection, connection strings and registered serversCurrentDatabaseis empty because the connection has never been opened, which is unchanged behaviour but deserves a deliberate check rather than an assumptionThis text was created by Claude and reviewed by Andreas Jordan.