【发布时间】:2010-05-02 13:51:46
【问题描述】:
我有以下 PowerShell 代码:
function Get-SmoConnection
{
param
([string] $serverName = "", [int] $connectionTimeout = 0)
if($serverName.Length -eq 0)
{
$serverConnection = New-Object `
Microsoft.SqlServer.Management.Common.ServerConnection
}
else
{
$serverConnection = New-Object `
Microsoft.SqlServer.Management.Common.ServerConnection($serverName)
}
if($connectionTimeout -ne 0)
{
$serverConnection.ConnectTimeout = $connectionTimeout
}
try
{
$serverConnection.Connect()
$serverConnection
}
catch [system.Management.Automation.MethodInvocationException]
{
$null
}
}
$connection = get-smoconnection "ServerName" 2
if($connection -ne $null)
{
Write-Host $connection.ServerInstance
Write-Host $connection.ConnectTimeout
}
else
{
Write-Host "Connection could not be established"
}
它似乎有效,除了尝试设置 SMO 连接超时的部分。如果连接成功,我可以验证 ServerConnection.ConnectTimeout 是否设置为 2(秒),但是当我为 SQL Server 实例提供虚假名称时,它仍会尝试连接到它约 15 秒(我相信默认超时值)。
有人有设置 SMO 连接超时的经验吗?提前谢谢你。
【问题讨论】:
-
我看到相同的行为:如果我将
StatementTimeout和ConnectTimeout都设置为 1(秒),然后尝试连接到不存在的服务器,则第一个超时需要大约 10 秒时间。但是,如果我立即重新尝试测试命令(连接到MasterDB),它会更快地超时,但仍然与设置不一致。烦人。
标签: sql-server-2008 powershell smo connection-timeout