国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

判斷WebBrowser瀏覽器網頁加載完成的處理方法

2024-07-09 22:47:11
字體:
來源:轉載
供稿:網友

很多人認為 SqlConnection 的連接是不耗時的,理由是循環執行 SqlConnection.Open 得到的平均時間幾乎為0,但每次首次open 時,耗時又往往達到幾個毫秒到幾秒不等,這又是為什么呢?

首先我們看一下 MSDN 上的權威文檔上是怎么說的

Connecting to a database server typically consists of several time-consuming steps. A physical channel such as a socket or a named pipe must be established, the initial handshake with the server must occur, the connection string information must be parsed, the connection must be authenticated by the server, checks must be run for enlisting in the current transaction, and so on.

以上摘自 http://msdn.microsoft.com/en-us/library/8xx3tyca%28VS.80%29.aspx

也就是說物理連接建立時,需要做和服務器握手,解析連接字符串,授權,約束的檢查等等操作,而物理連接建立后,這些操作就不會去做了。這些操作是需要一定的時間的。所以很多人喜歡用一個靜態對象存儲 SqlConnection 來始終保持物理連接,但采用靜態對象時,多線程訪問會帶來一些問題,實際上,我們完全不需要這么做,因為 SqlConnection 默認打開了連接池功能,當程序 執行  SqlConnection.Close 后,物理連接并不會被立即釋放,所以這才出現當循環執行 Open操作時,執行時間幾乎為0.

下面我們先看一下不打開連接池時,循環執行 SqlConnection.Open 的耗時

代碼如下:
public static void OpenWithoutPooling()
{
string connectionString = "Data Source=192.168.10.2; Initial Catalog=News;Integrated Security=True;Pooling=False;";
Stopwatch sw = new Stopwatch();
sw.Start();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
}
sw.Stop();
Console.WriteLine("Without Pooling, first connection elapsed {0} ms", sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < 100; i++)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
}
}
sw.Stop();
Console.WriteLine("Without Pooling, average connection elapsed {0} ms", sw.ElapsedMilliseconds / 100);
}

SqlConnection 默認是打開連接池的,如果要強制關閉,我們需要在連接字符串中加入 Pooling=False

調用程序如下:

代碼如下:
Test.SqlConnectionTest.OpenWithoutPooling();
Console.WriteLine("Waiting for 10s");
System.Threading.Thread.Sleep(10 * 1000);
Test.SqlConnectionTest.OpenWithoutPooling();
Console.WriteLine("Waiting for 600s");
System.Threading.Thread.Sleep(600 * 1000);
Test.SqlConnectionTest.OpenWithoutPooling();

下面是測試結果

Without Pooling, first connection elapsed 13 ms
Without Pooling, average connection elapsed 5 ms
Wating for 10s
Without Pooling, first connection elapsed 6 ms
Without Pooling, average connection elapsed 4 ms
Wating for 600s
Without Pooling, first connection elapsed 7 ms

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌拉特中旗| 上林县| 鞍山市| 丹东市| 兰考县| 南涧| 班戈县| 保康县| 潞西市| 新闻| 岳阳市| 贡嘎县| 盈江县| 双峰县| 汉寿县| 安义县| 弥勒县| 土默特右旗| 瑞昌市| 恭城| 乌兰浩特市| 彭州市| 无棣县| 抚州市| 晴隆县| 体育| 邢台市| 扎赉特旗| 阳东县| 阿勒泰市| 榆树市| 乌兰浩特市| 罗田县| 中江县| 固镇县| 杂多县| 仙桃市| 驻马店市| 营口市| 徐州市| 庆安县|