Skip to content

Commit bc92374

Browse files
Update-SqlPermission - Remove unnecessary SqlConnectionObject.Close() calls
The Close() calls were added as a workaround for SMO DataReader conflicts, but modern SMO properly manages its own connection lifecycle - all Enum* methods (EnumMembers, EnumServerPermissions, EnumDatabasePermissions, etc.) open and close their DataReaders internally via SqlDataAdapter.Fill(). The calls were harmless with normal pooled connections (Close() returns to the pool, next Open() gets a fresh connection) but catastrophic with DAC connections: each Close() terminates the single DAC TCP slot, and the reconnect attempt to ADMIN:server fails when the slot hasn't been freed yet, generating "max 1 dedicated administrator connections" errors. The worst case was the Close() pair inside the foreach ($role in $sourceDb.Roles) loop - called once per role per database per login, producing 30-40 DAC errors in the scenario reported in #10284. Fixes #10284 (do Sync-DbaAvailabilityGroup) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
1 parent 27e4da9 commit bc92374

1 file changed

Lines changed: 0 additions & 15 deletions

File tree

private/functions/Update-SqlPermission.ps1

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ function Update-SqlPermission {
5959
}
6060
}
6161

62-
# gotta close because enum repeatedly causes problems with the datareader
63-
$null = $SourceServer.ConnectionContext.SqlConnectionObject.Close()
64-
$null = $DestServer.ConnectionContext.SqlConnectionObject.Close()
65-
6662
# Server Roles: sysadmin, bulklogin, etc
6763
foreach ($role in $SourceServer.Roles) {
6864
$roleName = $role.Name
@@ -132,9 +128,6 @@ function Update-SqlPermission {
132128
Securables: Connect SQL, View any database, Administer Bulk Operations, etc.
133129
#>
134130

135-
$null = $sourceServer.ConnectionContext.SqlConnectionObject.Close()
136-
$null = $destServer.ConnectionContext.SqlConnectionObject.Close()
137-
138131
$perms = $SourceServer.EnumServerPermissions($loginName)
139132
foreach ($perm in $perms) {
140133
$permState = $perm.PermissionState
@@ -259,8 +252,6 @@ function Update-SqlPermission {
259252
}
260253
}
261254

262-
$null = $sourceDb.Parent.ConnectionContext.SqlConnectionObject.Close()
263-
$null = $destDb.Parent.ConnectionContext.SqlConnectionObject.Close()
264255
# Remove Connect, Alter Any Assembly, etc
265256
$destPerms = $destDb.EnumDatabasePermissions($newLoginName)
266257
$perms = $sourceDb.EnumDatabasePermissions($loginName)
@@ -292,9 +283,6 @@ function Update-SqlPermission {
292283
}
293284

294285
# Adding database mappings and securables
295-
$null = $SourceLogin.Parent.ConnectionContext.SqlConnectionObject.Close()
296-
$null = $DestServer.ConnectionContext.SqlConnectionObject.Close()
297-
298286
foreach ($db in $SourceLogin.EnumDatabaseMappings()) {
299287
$dbName = $db.DbName
300288
$destDb = $DestServer.Databases[$dbName]
@@ -355,8 +343,6 @@ function Update-SqlPermission {
355343
} else {
356344
# Database Roles: db_owner, db_datareader, etc
357345
foreach ($role in $sourceDb.Roles) {
358-
$null = $sourceDb.Parent.ConnectionContext.SqlConnectionObject.Close()
359-
$null = $destDb.Parent.ConnectionContext.SqlConnectionObject.Close()
360346
if ($role.EnumMembers() -contains $loginName) {
361347
$roleName = $role.Name
362348
$destDbRole = $destDb.Roles[$roleName]
@@ -375,7 +361,6 @@ function Update-SqlPermission {
375361
}
376362
}
377363
# Connect, Alter Any Assembly, etc
378-
$null = $sourceDb.Parent.ConnectionContext.SqlConnectionObject.Close()
379364
$perms = $sourceDb.EnumDatabasePermissions($loginName)
380365
foreach ($perm in $perms) {
381366
$permState = $perm.PermissionState

0 commit comments

Comments
 (0)