From d5201fea6372485f5af31640e7b128f910e03013 Mon Sep 17 00:00:00 2001 From: andreasjordan Date: Sun, 31 Mar 2024 13:47:56 +0200 Subject: [PATCH 1/3] Open connection to get CurrentDatabase --- public/Connect-DbaInstance.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/public/Connect-DbaInstance.ps1 b/public/Connect-DbaInstance.ps1 index 886a9ff359..c4a03b9b68 100644 --- a/public/Connect-DbaInstance.ps1 +++ b/public/Connect-DbaInstance.ps1 @@ -594,6 +594,8 @@ function Connect-DbaInstance { # Test if we have to copy the connection context # 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 + # Pooled connections are automatically closed, so we need to open the connection to get the ConnectionContext.CurrentDatabase + $null = $inputObject.ConnectionContext.ProcessID $copyContext = $false if ($Database -and $inputObject.ConnectionContext.CurrentDatabase -ne $Database) { Write-Message -Level Verbose -Message "Database provided. Does not match ConnectionContext.CurrentDatabase, copying ConnectionContext and setting the CurrentDatabase" From 160061bde70048b907031de7188a76035a3ae278 Mon Sep 17 00:00:00 2001 From: andreasjordan Date: Thu, 11 Apr 2024 21:26:46 +0200 Subject: [PATCH 2/3] fix issues with connection pooling --- public/Invoke-DbaDbDataMasking.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/Invoke-DbaDbDataMasking.ps1 b/public/Invoke-DbaDbDataMasking.ps1 index 17d1f6e494..983d91ea02 100644 --- a/public/Invoke-DbaDbDataMasking.ps1 +++ b/public/Invoke-DbaDbDataMasking.ps1 @@ -241,7 +241,7 @@ function Invoke-DbaDbDataMasking { foreach ($instance in $SqlInstance) { try { - $server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9 + $server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9 -NonPooledConnection } catch { Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue } From ffacb227f2416d3b050183645774fd7c439157b8 Mon Sep 17 00:00:00 2001 From: andreasjordan Date: Fri, 12 Apr 2024 12:15:23 +0200 Subject: [PATCH 3/3] connect only if needed --- public/Connect-DbaInstance.ps1 | 16 +++++++++++----- public/Invoke-DbaDbDataMasking.ps1 | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/public/Connect-DbaInstance.ps1 b/public/Connect-DbaInstance.ps1 index 35be6b5e70..27a9837627 100644 --- a/public/Connect-DbaInstance.ps1 +++ b/public/Connect-DbaInstance.ps1 @@ -594,12 +594,18 @@ function Connect-DbaInstance { # Test if we have to copy the connection context # 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 - # Pooled connections are automatically closed, so we need to open the connection to get the ConnectionContext.CurrentDatabase - $null = $inputObject.ConnectionContext.ProcessID $copyContext = $false - if ($Database -and $inputObject.ConnectionContext.CurrentDatabase -ne $Database) { - Write-Message -Level Verbose -Message "Database provided. Does not match ConnectionContext.CurrentDatabase, copying ConnectionContext and setting the CurrentDatabase" - $copyContext = $true + if ($Database) { + Write-Message -Level Debug -Message "Database [$Database] provided." + if (-not $inputObject.ConnectionContext.CurrentDatabase) { + Write-Message -Level Debug -Message "ConnectionContext.CurrentDatabase is empty, so connection will be opened to get the value" + $inputObject.ConnectionContext.Connect() + Write-Message -Level Debug -Message "ConnectionContext.CurrentDatabase is now [$($inputObject.ConnectionContext.CurrentDatabase)]" + } + 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 + } } if ($ApplicationIntent -and $inputObject.ConnectionContext.ApplicationIntent -ne $ApplicationIntent) { Write-Message -Level Verbose -Message "ApplicationIntent provided. Does not match ConnectionContext.ApplicationIntent, copying ConnectionContext and setting the ApplicationIntent" diff --git a/public/Invoke-DbaDbDataMasking.ps1 b/public/Invoke-DbaDbDataMasking.ps1 index 983d91ea02..17d1f6e494 100644 --- a/public/Invoke-DbaDbDataMasking.ps1 +++ b/public/Invoke-DbaDbDataMasking.ps1 @@ -241,7 +241,7 @@ function Invoke-DbaDbDataMasking { foreach ($instance in $SqlInstance) { try { - $server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9 -NonPooledConnection + $server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9 } catch { Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue }