Skip to content

Commit

Permalink
Only use GetDatabaseConnection if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan committed Jun 20, 2024
1 parent 012410b commit 47aff04
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion public/Connect-DbaInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ function Connect-DbaInstance {
# Currently only if we have a different Database or have to switch to a NonPooledConnection or using a specific StatementTimeout or using ApplicationIntent
# We do not test for SqlCredential as this would change the behavior compared to the legacy code path
$copyContext = $false
$dbChanged = $false
if ($Database) {
Write-Message -Level Debug -Message "Database [$Database] provided."
if (-not $inputObject.ConnectionContext.CurrentDatabase) {
Expand All @@ -600,6 +601,7 @@ function Connect-DbaInstance {
if ($inputObject.ConnectionContext.CurrentDatabase -ne $Database) {
Write-Message -Level Verbose -Message "Database [$Database] provided. Does not match ConnectionContext.CurrentDatabase [$($inputObject.ConnectionContext.CurrentDatabase)], copying ConnectionContext and setting the CurrentDatabase"
$copyContext = $true
$dbChanged = $true
}
}
if ($ApplicationIntent -and $inputObject.ConnectionContext.ApplicationIntent -ne $ApplicationIntent) {
Expand Down Expand Up @@ -634,7 +636,7 @@ function Connect-DbaInstance {
$connContext.ServerInstance = 'ADMIN:' + $connContext.ServerInstance
$connContext.NonPooledConnection = $true
}
if ($Database) {
if ($dbChanged) {
# Save StatementTimeout because it might be reset on GetDatabaseConnection
$savedStatementTimeout = $connContext.StatementTimeout
$connContext = $connContext.GetDatabaseConnection($Database)
Expand Down

0 comments on commit 47aff04

Please sign in to comment.