Skip to content

Connect-DbaInstance - Set NonPooledConnection on ServerConnection for…#10260

Merged
potatoqualitee merged 1 commit intodataplat:developmentfrom
mbentham:development
Mar 18, 2026
Merged

Connect-DbaInstance - Set NonPooledConnection on ServerConnection for…#10260
potatoqualitee merged 1 commit intodataplat:developmentfrom
mbentham:development

Conversation

@mbentham
Copy link
Contributor

@mbentham mbentham commented Mar 17, 2026

Type of Change

Purpose

Fix SQL Server error 17810 ("maximum number of dedicated administrator connections already exists") when using Connect-DbaInstance -DedicatedAdminConnection. The DAC connection is not kept open between SMO operations because ServerConnection.NonPooledConnection is never set in the String input path, causing rapid connect/disconnect cycling that races against SQL Server's DAC slot release.

Approach

Added $server.ConnectionContext.NonPooledConnection = $true in the String input path of Connect-DbaInstance, alongside the existing ConnectionContext property assignments (BatchSeparator, LockTimeout, etc.). This is gated on $NonPooledConnection which covers both -DedicatedAdminConnection and explicit -NonPooledConnection usage.

The same pattern already exists in the Server/copy-context path (line ~711) and in New-DbaConnectionString (line ~465) — this change brings the String input path in line with both.

Commands to test

# Clear the error log first so results are unambiguous
Invoke-DbaQuery -SqlInstance localhost -Query "EXEC sp_cycle_errorlog"

# Connect via DAC, run a query, disconnect
$server = Connect-DbaInstance -SqlInstance localhost -DedicatedAdminConnection
Invoke-DbaQuery -SqlInstance $server -Query "SELECT @@servername"
$server | Disconnect-DbaInstance

# Check SQL Server error log for 17810 - should return no rows
Invoke-DbaQuery -SqlInstance localhost -Query "EXEC xp_readerrorlog 0, 1, N'17810'"

Before fix: xp_readerrorlog returns rows showing error 17810 logged during the DAC session (sometimes, depending on how quickly your particular SQL Server cleaned up the closing DAC connection - its an intermittant race condition).
After fix: No 17810 entries — the DAC connection stays open for the duration instead of cycling.

Learning

  • SqlConnectionInfo.Pooled and ServerConnection.NonPooledConnection are independent settings. Pooled = $false controls SqlClient connection pooling (Pooling=false in the connection string), while NonPooledConnection = $true controls whether SMO auto-disconnects after each operation. Both are required for DAC connections to work correctly.

… DAC and NonPooled connections

SqlConnectionInfo.Pooled = $false only sets Pooling=false in the connection
string, but does not set ServerConnection.NonPooledConnection = $true.
Without this, SMO auto-disconnects after each operation, causing rapid
DAC open/close cycles that trigger SQL Server error 17810 when the next
auto-connect races the prior disconnect.

(do Connect-DbaInstance)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@andreasjordan
Copy link
Collaborator

I can run your "commands to test" even without your fix. So I don't see the problem.

image

@mbentham
Copy link
Contributor Author

mbentham commented Mar 18, 2026

Yeah sorry I should have been clearer its a tricky one - the DBAtools commands run fine with no errors in powershell without the fix, but theres a race condition caused by cycling the connection that causes the SQL Server instance to log error 17810 "maximum number of dedicated administrator connections already exists" to the SQL Server log file if the the DAC connection isnt cleaned up quickly enough by SQL Server before the next DBATools command is sent, often the DBAtools command still succeeds anyway due to the automatic retry in SqlClient that SMO, and therefore the Connect-DbaInstance tool inherits. But if anyone is checking the sql server error logs, or has a monitoring tool that flags server errors they'll see the errors caused by this module.

I've improved the commands to test section

@andreasjordan
Copy link
Collaborator

Ok, so I need to create a loop and open/close the connection multiple times to see the problem?

But I still see no change, as $server.ConnectionContext.NonPooledConnection = $true has no effect as $server.ConnectionContext.NonPooledConnection is already $true without the change. Or am I missing something?

@andreasjordan
Copy link
Collaborator

Ok, with a loop, I can reproduce the issue:

foreach ($i in 1..1000) {
    $dac = Connect-DbaInstance -SqlInstance fci01 -DedicatedAdminConnection
    $null = Invoke-DbaQuery -SqlInstance $dac -Query "SELECT @@servername"
    $null = $dac | Disconnect-DbaInstance
}

And with your fix, the issue really seems to be resolved.

Copy link
Collaborator

@andreasjordan andreasjordan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested it - it works! Thanks for your contribution.

@mbentham
Copy link
Contributor Author

Brilliant thanks, I'll be sure to be clearer in the PR next time, save the confusion.

@mbentham mbentham closed this Mar 18, 2026
@mbentham mbentham reopened this Mar 18, 2026
@potatoqualitee potatoqualitee merged commit 9a4e4ba into dataplat:development Mar 18, 2026
6 checks passed
@potatoqualitee
Copy link
Member

Thank you so much! 🔥 🚒

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connect-DbaInstance - DedicatedAdminConnection causes SQL Server error 17810 due to missing NonPooledConnection on ServerConnection

3 participants