Skip to content

Commit 43d76cf

Browse files
Fix DAC connection reuse detection causing excessive DAC errors in Sync-DbaAvailabilityGroup
The dacConnected check in multiple commands was checking Server.Name for the ADMIN: prefix, but the ADMIN: prefix lives in ConnectionContext.ServerInstance. Since Server.Name never starts with ADMIN:, the check always returned false - causing commands to mark $dacOpened = $true and disconnect the shared DAC connection at their end blocks. This resulted in Sync-DbaAvailabilityGroup's DAC being unintentionally closed by each sub-command (Copy-DbaCredential, Copy-DbaDbMail, Copy-DbaLinkedServer). Additionally, Sync-DbaLoginPermission was passed the DAC server object. Update-SqlPermission calls SqlConnectionObject.Close() in tight loops (per role per database per login), causing repeated DAC slot acquire/release cycles that trigger "max 1 DAC connections" SQL errors. Fixes: - Change all dacConnected checks from .InputObject.Name to .InputObject.ConnectionContext.ServerInstance (or .Parent.ConnectionContext.ServerInstance) - In Sync-DbaAvailabilityGroup, pass DomainInstanceName string to Sync-DbaLoginPermission so it opens a normal pooled connection instead of reusing the DAC server Fixes #10284 (do *AvailabilityGroup*, *Credential*, *LinkedServer*, *DbMail*, *Migration*, *Decrypt*) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
1 parent 562e3ac commit 43d76cf

9 files changed

Lines changed: 21 additions & 10 deletions

public/Copy-DbaCredential.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function Copy-DbaCredential {
156156
if ($ExcludePassword) { $dacNeeded = $false } else { $dacNeeded = $true }
157157

158158
# Do we have a dedicated admin connection already?
159-
$dacConnected = $Source.Type -eq 'Server' -and $Source.InputObject.Name -match '^ADMIN:'
159+
$dacConnected = $Source.Type -eq 'Server' -and $Source.InputObject.ConnectionContext.ServerInstance -match '^ADMIN:'
160160

161161
$dacOpened = $false
162162
if ($dacNeeded) {

public/Copy-DbaDbMail.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ function Copy-DbaDbMail {
390390
if ($ExcludePassword) { $dacNeeded = $false } else { $dacNeeded = $true }
391391

392392
# Do we have a dedicated admin connection already?
393-
$dacConnected = $Source.Type -eq 'Server' -and $Source.InputObject.Name -match '^ADMIN:'
393+
$dacConnected = $Source.Type -eq 'Server' -and $Source.InputObject.ConnectionContext.ServerInstance -match '^ADMIN:'
394394

395395
$dacOpened = $false
396396
if ($dacNeeded) {

public/Copy-DbaLinkedServer.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ function Copy-DbaLinkedServer {
307307
if ($ExcludePassword) { $dacNeeded = $false } else { $dacNeeded = $true }
308308

309309
# Do we have a dedicated admin connection already?
310-
$dacConnected = $Source.Type -eq 'Server' -and $Source.InputObject.Name -match '^ADMIN:'
310+
$dacConnected = $Source.Type -eq 'Server' -and $Source.InputObject.ConnectionContext.ServerInstance -match '^ADMIN:'
311311

312312
$dacOpened = $false
313313
if ($dacNeeded) {

public/Export-DbaCredential.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function Export-DbaCredential {
115115
if ($ExcludePassword) { $dacNeeded = $false } else { $dacNeeded = $true }
116116

117117
# Do we have a dedicated admin connection already?
118-
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.Name -match '^ADMIN:'
118+
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.ConnectionContext.ServerInstance -match '^ADMIN:'
119119

120120
$dacOpened = $false
121121
if ($dacNeeded) {

public/Export-DbaInstance.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ function Export-DbaInstance {
240240
if ($ExcludePassword) { $dacNeeded = $false }
241241

242242
# Do we have a dedicated admin connection already?
243-
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.Name -match '^ADMIN:'
243+
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.ConnectionContext.ServerInstance -match '^ADMIN:'
244244

245245
$dacOpened = $false
246246
if ($dacNeeded) {

public/Export-DbaLinkedServer.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function Export-DbaLinkedServer {
129129
if ($ExcludePassword) { $dacNeeded = $false } else { $dacNeeded = $true }
130130

131131
# Do we have a dedicated admin connection already?
132-
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.Name -match '^ADMIN:'
132+
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.ConnectionContext.ServerInstance -match '^ADMIN:'
133133

134134
$dacOpened = $false
135135
if ($dacNeeded) {

public/Invoke-DbaDbDecryptObject.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ function Invoke-DbaDbDecryptObject {
198198
# Try to connect to instance
199199
try {
200200
# Do we have a dedicated admin connection already?
201-
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.Name -match '^ADMIN:'
201+
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.ConnectionContext.ServerInstance -match '^ADMIN:'
202202
$dacOpened = $false
203203
if ($dacConnected) {
204204
Write-Message -Level Verbose -Message "Reusing dedicated admin connection."

public/Start-DbaMigration.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ function Start-DbaMigration {
366366
if ($ExcludePassword) { $dacNeeded = $false }
367367

368368
# Do we have a dedicated admin connection already?
369-
$dacConnected = $Source.Type -eq 'Server' -and $Source.InputObject.Name -match '^ADMIN:'
369+
$dacConnected = $Source.Type -eq 'Server' -and $Source.InputObject.ConnectionContext.ServerInstance -match '^ADMIN:'
370370

371371
$dacOpened = $false
372372
if ($dacNeeded) {

public/Sync-DbaAvailabilityGroup.ps1

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function Sync-DbaAvailabilityGroup {
194194
if ($ExcludePassword) { $dacNeeded = $false } else { $dacNeeded = $true }
195195

196196
# Do we have a dedicated admin connection already?
197-
$dacConnected = $InputObject.Parent.Name -match '^ADMIN:'
197+
$dacConnected = $InputObject.Parent.ConnectionContext.ServerInstance -match '^ADMIN:'
198198

199199
if ($dacNeeded -and -not $dacConnected) {
200200
Stop-Function -Message "Pipeline source must use a dedicated admin connection to retrieve passwords. Use -ExcludePassword to bypass this requirement if you don't need passwords."
@@ -399,7 +399,18 @@ function Sync-DbaAvailabilityGroup {
399399

400400
if ($Exclude -notcontains "LoginPermissions") {
401401
Write-ProgressHelper -Activity $activity -StepNumber ($stepCounter++) -Message "Syncing login permissions"
402-
Sync-DbaLoginPermission -Source $server -Destination $secondaries -Login $Login -ExcludeLogin $ExcludeLogin
402+
# Use DomainInstanceName (string) instead of the server object to ensure Sync-DbaLoginPermission
403+
# opens a normal (non-DAC) connection. Update-SqlPermission calls SqlConnectionObject.Close()
404+
# in loops, which on a DAC connection causes repeated DAC slot acquire/release cycles
405+
# that generate "max 1 DAC connections" errors in the SQL Error Log.
406+
$splatLoginPermission = @{
407+
Source = $server.DomainInstanceName
408+
SourceSqlCredential = $PrimarySqlCredential
409+
Destination = $secondaries
410+
Login = $Login
411+
ExcludeLogin = $ExcludeLogin
412+
}
413+
Sync-DbaLoginPermission @splatLoginPermission
403414
}
404415
}
405416

0 commit comments

Comments
 (0)