Created
August 18, 2017 07:43
-
-
Save skokhanovskiy/3093db4b745124f75a1f6c73667b5ee9 to your computer and use it in GitHub Desktop.
This file contains 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
$Server = 'SQL-APPS' | |
$ConnectionTimeout = 15 | |
$MaxConnections = 500 | |
$ConnectionStringFormat = 'Server = {0}; Connection Timeout = {1}; Max Pool Size = {2}; Integrated Security = True;' | |
$CommandQuery = 'SELECT 1' | |
$ConnectionString = $ConnectionStringFormat -f $Server, $ConnectionTimeout, $MaxConnections | |
$Connections = [System.Collections.ArrayList] @() | |
for ($I = 0; $I -lt $MaxConnections; $I++) | |
{ | |
[Void] $Connections.Add([System.Data.SqlClient.SqlConnection]::new($ConnectionString)) | |
} | |
for ($I = 0; $I -lt $Connections.Count; $I++) | |
{ | |
$Connection = $Connections[$I] | |
try | |
{ | |
Write-Verbose "Open connection #$I." -Verbose | |
$Connection.Open() | |
$Command = [System.Data.SqlClient.SqlCommand]::new($CommandQuery, $Connection) | |
try | |
{ | |
$Adapter = [System.Data.SqlClient.SqlDataAdapter]::new() | |
$Adapter.SelectCommand = $Command | |
try | |
{ | |
$DataSet = [System.Data.DataSet]::new() | |
try | |
{ | |
Write-Verbose "Invoke query." -Verbose | |
[Void] $Adapter.Fill($DataSet) | |
} | |
finally | |
{ | |
$DataSet.Dispose() | |
} | |
} | |
finally | |
{ | |
$Adapter.Dispose() | |
} | |
} | |
finally | |
{ | |
$Command.Dispose() | |
} | |
} | |
catch | |
{ | |
$_ | Write-Error | |
break | |
} | |
} | |
Read-Host -Prompt 'Press Enter to close all connections' | |
for ($I = 0; $I -lt $Connections.Count; $I++) | |
{ | |
$Connection = $Connections[$I] | |
if ($Connection) | |
{ | |
if ($Connection.State -eq [System.Data.ConnectionState]::Open) | |
{ | |
Write-Verbose "Close connection #$I." -Verbose | |
$Connection.Close() | |
[System.Data.SqlClient.SqlConnection]::ClearPool($Connection) | |
} | |
$Connection.Dispose() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment