6

I have a website using Microsoft SQL 2008 server over local network. Sometimes, SQL server machine is rebooted, and so the website fails to connect to the database. If the machine is up and running, it will respond fast. If it's down, there is no need to wait for 15 seconds. 3 seconds are ok.

I want to display apologizes on the website when the database is not reachable, and want to do it fast. But setting Connection Timeout=3 in connection string seems having no effect. The page spends 22 seconds to wait before throwing SqlException on SqlConnection.Open();.

What's wrong with it? May it be a hidden configuration which overrides the timeout?

Currently, my connection string is

Data Source=...;
Initial Catalog=...;
Integrated Security=True;
Connection Timeout=3

If I set it to ...;ConnectionTimeout=3 (without space),

System.ArgumentException: Keyword not supported: 'connectiontimeout'.

is thrown (strange, MSDN documentation indicates that we can use both strings).

4

3 に答える 3

2

ネットワーク ハードウェアがネットワーク ドライバーに接続タイムアウトを報告するまでにタイムアウトがあり、ネットワーク IO を待機しているプログラムに通知されます。telnet サーバー名 1433 を介してトランスポート層のタイムアウトを確認できます (SQL サーバーがポート 1433 でリッスンしていると仮定します)。

しかし、プロセスがネットワーク API を初期化し (Web アプリが独自のアプリケーション プールにあると仮定)、要求を送信し、ハードウェアがタイムアウトするのを待つには 3 秒では短すぎます。BIOS/ファームウェア/ドライバーを更新しても、おそらく応答時間はそれほど短縮されません。

接続は非同期で行ったほうがよいでしょう。不運なユーザーは、データベースがダウンしているときに応答を確認するために 3 秒間待つ必要がある可能性があるため、EndInvoke を使用して非同期呼び出しを終了することはお勧めしません。多分 Ajax 呼び出しの方が良いでしょう。多くのユーザーが常に Web サイトにアクセスしている場合、接続チェックの結果をキャッシュし、ユーザーにとって意味のある方法で更新することができます。

于 2010-06-28T20:22:00.233 に答える
2

次のブログ投稿は、この問題を解決するのに役立ちました: http://improve.dk/controlling-sqlconnection-timeouts/

于 2013-09-10T04:43:15.257 に答える
1

ConnectionTimeoutスペースなしは、接続文字列ではなく、コード経由でアクセスする場合のプロパティ名です。

これが役立つかどうかはわかりませんが、過去にこの問題に遭遇したときは、 も設定する必要があったためですSqlCommand.CommandTimeout。私に起こったことは、接続が正常に開かれた後、DB サーバーがダウンし、次のコマンドが接続タイムアウトに基づいて期待したほど速くタイムアウトしなかったことです。これは、CommandTimeout設定する必要があるためでもありました。

于 2010-06-24T21:48:17.827 に答える