Skip to content

Commit

Permalink
Connect DAC to test connection as soon as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan committed Mar 31, 2024
1 parent 86b3f3d commit 3541cec
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions public/Connect-DbaInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -948,14 +948,12 @@ function Connect-DbaInstance {
# But ConnectionContext.IsOpen does not tell the truth if the instance was just shut down
# And we don't use $server.ConnectionContext.Connect() as this would create a non pooled connection
# Instead we run a real T-SQL command and just SELECT something to be sure we have a valid connection and let the SMO handle the connection
if (-not $DedicatedAdminConnection) {
try {
Write-Message -Level Debug -Message "We connect to the instance by running SELECT 'dbatools is opening a new connection'"
$null = $server.ConnectionContext.ExecuteWithResults("SELECT 'dbatools is opening a new connection'")
Write-Message -Level Debug -Message "We have a connected server object"
} catch {
Stop-Function -Target $instance -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Continue
}
try {
Write-Message -Level Debug -Message "We connect to the instance by running SELECT 'dbatools is opening a new connection'"
$null = $server.ConnectionContext.ExecuteWithResults("SELECT 'dbatools is opening a new connection'")
Write-Message -Level Debug -Message "We have a connected server object"
} catch {
Stop-Function -Target $instance -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Continue
}

if ($AzureUnsupported -and $server.DatabaseEngineType -eq "SqlAzureDatabase") {
Expand Down

0 comments on commit 3541cec

Please sign in to comment.