-
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
Merged
johlju
merged 18 commits into
dsccommunity:main
from
dan-hughes:sql-connection-optimization
Jan 26, 2026
Merged
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
531be16
Remove explicit disconnect as there was no explicit connect
dan-hughes d6c6633
Create a connection object to create a server connection
dan-hughes 1753689
Update changelog
dan-hughes 1ca5485
Add output type
dan-hughes 4bbb076
Remove copilot instruction
dan-hughes b08129f
Fix HQRM
dan-hughes 78dfa00
Fix tests
dan-hughes 5afe316
Move ServerConnection to correct namespace
dan-hughes 9896500
Update tests to use correct type for ServerConnection
dan-hughes 6bd572d
Add missing ctor and method
dan-hughes 67e37a4
Put refresh in correct class
dan-hughes 8663f19
Add set to allow test use
dan-hughes 85cc72f
Revert to using New-Object
dan-hughes 46d2a8f
Debug Connect-Sql
dan-hughes 2dcc3f7
Fix HQRM
dan-hughes 32ea5d6
Trigger tests hopefully
dan-hughes c1949b3
More debug
dan-hughes 07f78cd
Update output type
dan-hughes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')] | ||
|
|
@@ -127,8 +128,7 @@ function Connect-Sql | |
| $databaseEngineInstance = '{0}:{1}' -f $Protocol, $databaseEngineInstance | ||
| } | ||
|
|
||
| $sqlServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' | ||
| $sqlConnectionContext = $sqlServerObject.ConnectionContext | ||
| $sqlConnectionContext = [Microsoft.SqlServer.Management.Common.ServerConnection]::new() | ||
| $sqlConnectionContext.ServerInstance = $databaseEngineInstance | ||
| $sqlConnectionContext.StatementTimeout = $StatementTimeout | ||
| $sqlConnectionContext.ConnectTimeout = $StatementTimeout | ||
|
|
@@ -179,14 +179,16 @@ function Connect-Sql | |
| { | ||
| $onlineStatus = 'Online' | ||
| $connectTimer = [System.Diagnostics.StopWatch]::StartNew() | ||
| $sqlConnectionContext.Connect() | ||
| $sqlServerObject = [Microsoft.SqlServer.Management.Smo.Server]::new($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 +272,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 |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.