【问题标题】:Sql Server SMO connection timeout not workingSql Server SMO 连接超时不起作用
【发布时间】: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 连接超时的经验吗?提前谢谢你。

【问题讨论】:

  • 我看到相同的行为:如果我将 StatementTimeoutConnectTimeout 都设置为 1(秒),然后尝试连接到不存在的服务器,则第一个超时需要大约 10 秒时间。但是,如果我立即重新尝试测试命令(连接到Master DB),它会更快地超时,但仍然与设置不一致。烦人。

标签: sql-server-2008 powershell smo connection-timeout


【解决方案1】:

我似乎无法重现您所看到的行为。如果将您的函数重新创建为脚本而不是函数,则无论服务器名称参数是否伪造,ConnectionTimeout 属性似乎都有效:

Measure-Command {./get-smoconnection.ps1 'Z03\sq2k8' 2}

【讨论】:

  • 感谢您测试我的代码。与此同时,我尝试了与它等效的 C# - 设置超时并尝试连接到不存在的服务器,它可以工作。我需要在另一台机器上测试 PowerShell 脚本,但我怀疑它也可以工作 - 我不知道我可以做些什么来搞乱我的测试计算机,这让我发疯了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
  • 2016-01-25
  • 2013-04-03
  • 1970-01-01
相关资源
最近更新 更多