-
Notifications
You must be signed in to change notification settings - Fork 227
Sql Connection Optimization #2433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
531be16
d6c6633
1753689
1ca5485
4bbb076
b08129f
78dfa00
5afe316
9896500
6bd572d
67e37a4
8663f19
85cc72f
46d2a8f
2dcc3f7
32ea5d6
c1949b3
07f78cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ | |
| function Connect-Sql | ||
| { | ||
| [CmdletBinding(DefaultParameterSetName = 'SqlServer')] | ||
| [OutputType([Microsoft.SqlServer.Management.Smo.Server])] | ||
| param | ||
| ( | ||
| [Parameter(ParameterSetName = 'SqlServer')] | ||
|
|
@@ -92,6 +93,8 @@ function Connect-Sql | |
| $Encrypt | ||
| ) | ||
|
|
||
| [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.Location -like '*SqlServer*' } | ForEach-Object { Write-Verbose ("GAC:{0}`t`tVersion:{1}`t`tLocation:{2}" -f $_.GlobalAssemblyCache, $_.ImageRuntimeVersion, $_.Location) -Verbose } | ||
|
|
||
| Import-SqlDscPreferredModule | ||
johlju marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <# | ||
|
|
@@ -127,8 +130,7 @@ function Connect-Sql | |
| $databaseEngineInstance = '{0}:{1}' -f $Protocol, $databaseEngineInstance | ||
| } | ||
|
|
||
| $sqlServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | ||
| $sqlConnectionContext = $sqlServerObject.ConnectionContext | ||
| $sqlConnectionContext = New-Object -TypeName Microsoft.SqlServer.Management.Common.ServerConnection | ||
| $sqlConnectionContext.ServerInstance = $databaseEngineInstance | ||
| $sqlConnectionContext.StatementTimeout = $StatementTimeout | ||
| $sqlConnectionContext.ConnectTimeout = $StatementTimeout | ||
|
|
@@ -179,14 +181,16 @@ function Connect-Sql | |
| { | ||
| $onlineStatus = 'Online' | ||
| $connectTimer = [System.Diagnostics.StopWatch]::StartNew() | ||
| $sqlConnectionContext.Connect() | ||
| $sqlServerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $sqlConnectionContext | ||
|
|
||
| <# | ||
| The addition of the ConnectTimeout property to the ConnectionContext will force the | ||
| Connect() method to block until successful. THe SMO object's Status property may not | ||
| report 'Online' immediately even though the Connect() was successful. The loop is to | ||
| ensure the SMO's Status property was been updated. | ||
| The addition of the ConnectTimeout property to the ConnectionContext will force the | ||
| Connect() method to block until successful. The SMO object's Status property may not | ||
| report 'Online' immediately even though the Connect() was successful. The loop is to | ||
| ensure the SMO's Status property was been updated. | ||
| #> | ||
| $sqlServerObject.ConnectionContext.Connect() | ||
|
|
||
| $sleepInSeconds = 2 | ||
| do | ||
| { | ||
|
|
@@ -270,12 +274,12 @@ function Connect-Sql | |
| { | ||
| $connectTimer.Stop() | ||
| <# | ||
| Connect will ensure we actually can connect, but we need to disconnect | ||
| Connect() will ensure we actually can connect, but we need to disconnect | ||
| from the session so we don't have anything hanging. If we need run a | ||
| method on the returned $sqlServerObject it will automatically open a | ||
| new session and then close, therefore we don't need to keep this | ||
| session open. | ||
| #> | ||
| $sqlConnectionContext.Disconnect() | ||
| $sqlServerObject.ConnectionContext.Disconnect() | ||
|
Comment on lines
273
to
+281
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard against null objects in 🐛 Proposed fix finally
{
- $connectTimer.Stop()
+ if ($null -ne $connectTimer)
+ {
+ $connectTimer.Stop()
+ }
<#
Connect() will ensure we actually can connect, but we need to disconnect
from the session so we don't have anything hanging. If we need run a
method on the returned $sqlServerObject it will automatically open a
new session and then close, therefore we don't need to keep this
session open.
#>
- $sqlServerObject.ConnectionContext.Disconnect()
+ if ($null -ne $sqlServerObject -and $null -ne $sqlServerObject.ConnectionContext)
+ {
+ $sqlServerObject.ConnectionContext.Disconnect()
+ }
}🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.