`
sailorlee
  • 浏览: 41605 次
  • 性别: Icon_minigender_1
  • 来自: 河北唐山
最近访客 更多访客>>
社区版块
存档分类
最新评论

Close, Dispose, using And Connection Pool

阅读更多

 

越來越搞不懂,除了該不該Dispose?如果用Dispose,還需要先Close嗎?
還有Dispose會不會影響Connection Pool呢?

哪來那麼多麻煩呀,真受不了 ~__~

寫了測試程式,嘗試從結果來找到一些解答,用10個thread給它connection開開關關1000次,我想特別是針對第3點,如果Dispose會影響Pool ,那Dispose會降低很多效能才是,畢竟開關是很花資源的。

得出來的數字如下:
無 Dispose 且打開 Pooling: 00:00:01.0625000
用 Using   且打開 Pooling: 00:00:01.1718750
有 Dispose 且打開 Pooling: 00:00:01.6562500
有 Dispose 且關掉 Pooling: 00:00:43.5937500
用 Using   且關掉 Pooling: 00:00:46.3906250
無 Dispose 且關掉 Pooling: 00:00:48.0312500

心得:
1、ㄟ,有沒有 Pooling還差真多,ado.net 內建啟動算是良善之舉。
2、Dispose 似乎不會影響Pooling,雖然他比較慢一點,但也表示他有在做事吧(乖乖資源回收?),如果能像己前寫dos程式的時代,可以查對記憶體的影響就好了。
3、using 會比較快耶,是因為少做close的動作嗎? 

附上測試用的code,下次哪個無聊的夜晚,再來繼續改進

 

 
1 namespace testApp {  
2     public partial class Form1 : Form {  
3         public Form1() {  
4             InitializeComponent();  
5         }  
6         System.Object lockThis = new System.Object();  
7     
8         public int count = 0;  
9         DateTime beginTime;  
10         DateTime endTime;  
11         int threadCount = 10;  
12         int testTimePerThread = 1000;  
13         int testCount;  
14         string connStr = "server=localhost;database=testdb;uid=sa;pwd=";  
15         string connStrWithoutPool = "server=localhost;database=testdb;uid=sa;pwd=;pooling=false";  
16         private void btnNoDispose_Click(object sender, EventArgs e) {  
17             testCount = threadCount * testTimePerThread;  
18             ArrayList threadList = null;  
19             // test dispose and pool = true  
20             count = 0;  
21             threadList = new ArrayList();  
22             for (int i = 0; i < threadCount; i++) {  
23                 Thread thread = new Thread(new ThreadStart(DisposePoolingTrueCallback));  
24                 threadList.Add(thread);  
25             }  
26             beginTime = DateTime.Now;  
27             foreach (Thread thread in threadList) {  
28                 if (thread.ThreadState != System.Threading.ThreadState.Running) {  
29                     thread.Start();  
30                 }  
31             }  
32             while (count < testCount) {  
33             }  
34             // test no dispose and pool = true  
35             count = 0;  
36             threadList = new ArrayList();  
37             for (int i = 0; i < threadCount; i++) {  
38                 Thread thread = new Thread(new ThreadStart(NoDisposePoolingTrueCallback));  
39                 threadList.Add(thread);  
40             }  
41             beginTime = DateTime.Now;  
42             foreach (Thread thread in threadList) {  
43                 if (thread.ThreadState != System.Threading.ThreadState.Running) {  
44                     thread.Start();  
45                 }  
46             }  
47             while (count < testCount) {  
48             }  
49             // test dispose and pool = false  
50             count = 0;  
51             threadList = new ArrayList();  
52             for (int i = 0; i < threadCount; i++) {  
53                 Thread thread = new Thread(new ThreadStart(DisposePoolingFalseCallback));  
54                 threadList.Add(thread);  
55             }  
56             beginTime = DateTime.Now;  
57             foreach (Thread thread in threadList) {  
58                 if (thread.ThreadState != System.Threading.ThreadState.Running) {  
59                     thread.Start();  
60                 }  
61             }  
62             while (count < testCount) {  
63             }  
64             // test no dispose and pool = false  
65             count = 0;  
66             threadList = new ArrayList();  
67             for (int i = 0; i < threadCount; i++) {  
68                 Thread thread = new Thread(new ThreadStart(NoDisposePoolingFalseCallback));  
69                 threadList.Add(thread);  
70             }  
71             beginTime = DateTime.Now;  
72             foreach (Thread thread in threadList) {  
73                 if (thread.ThreadState != System.Threading.ThreadState.Running) {  
74                     thread.Start();  
75                 }  
76             }  
77             while (count < testCount) {  
78             }  
79             // using and pool = true  
80             count = 0;  
81             threadList = new ArrayList();  
82             for (int i = 0; i < threadCount; i++) {  
83                 Thread thread = new Thread(new ThreadStart(UsingPoolingTrueCallback));  
84                 threadList.Add(thread);  
85             }  
86             beginTime = DateTime.Now;  
87             foreach (Thread thread in threadList) {  
88                 if (thread.ThreadState != System.Threading.ThreadState.Running) {  
89                     thread.Start();  
90                 }  
91             }  
92             while (count < testCount) {  
93             }  
94             // using and pool = false  
95             count = 0;  
96             threadList = new ArrayList();  
97             for (int i = 0; i < threadCount; i++) {  
98                 Thread thread = new Thread(new ThreadStart(UsingPoolingFalseCallback));  
99                 threadList.Add(thread);  
100             }  
101             beginTime = DateTime.Now;  
102             foreach (Thread thread in threadList) {  
103                 if (thread.ThreadState != System.Threading.ThreadState.Running) {  
104                     thread.Start();  
105                 }  
106             }  
107             while (count < testCount) {  
108             }  
109         }  
110  
111         /// <summary>  
112         /// 有 Dispose 且打開 Pooling  
113         /// </summary>  
114         private void DisposePoolingTrueCallback() {  
115             for (int i = 0; i < testTimePerThread; i++) {  
116                 SqlConnection conn = new SqlConnection();  
117                 conn.ConnectionString = connStr;  
118                 try {  
119                     conn.Open();                      
120                 } finally {  
121                     conn.Close();  
122                     conn.Dispose();  
123                 }  
124                 lock (lockThis) {  
125                     count++;  
126                     if (count == testCount) {  
127                         endTime = System.DateTime.Now;  
128                         Debug.Write("有 Dispose 且打開 Pooling: " + (endTime - beginTime) + "\n");  
129                     }  
130                 }  
131             }  
132         }  
133  
134         /// <summary>  
135         /// 無 Dispose 且打開 Pooling  
136         /// </summary>  
137         private void NoDisposePoolingTrueCallback() {  
138             for (int i = 0; i < testTimePerThread; i++) {  
139                 SqlConnection conn = new SqlConnection();  
140                 conn.ConnectionString = connStr;  
141                 try {  
142                     conn.Open();                      
143                 } finally {  
144                     conn.Close();  
145                 }  
146                 lock (lockThis) {  
147                     count++;  
148                     if (count == testCount) {  
149                         endTime = System.DateTime.Now;  
150                         Debug.Write("無 Dispose 且打開 Pooling: " + (endTime - beginTime) + "\n");  
151                     }  
152                 }  
153             }  
154         }  
155  
156         /// <summary>  
157         /// 有 Dispose 且關掉 Pooling  
158         /// </summary>  
159         private void DisposePoolingFalseCallback() {  
160             for (int i = 0; i < testTimePerThread; i++) {  
161                 SqlConnection conn = new SqlConnection();  
162                 conn.ConnectionString = connStrWithoutPool;  
163                 try {  
164                     conn.Open();                      
165                 } finally {  
166                     conn.Close();  
167                     conn.Dispose();  
168                 }  
169                 lock (lockThis) {  
170                     count++;  
171                     if (count == testCount) {  
172                         endTime = System.DateTime.Now;  
173                         //Console.Write("Bingo");  
174                         Debug.Write("有 Dispose 且關掉 Pooling: " + (endTime - beginTime) + "\n");  
175                     }  
176                 }  
177             }  
178         }  
179  
180         /// <summary>  
181         /// 無 Dispose 且關掉 Pooling  
182         /// </summary>  
183         private void NoDisposePoolingFalseCallback() {  
184             for (int i = 0; i < testTimePerThread; i++) {  
185                 SqlConnection conn = new SqlConnection();  
186                 conn.ConnectionString = connStrWithoutPool;  
187                 try {  
188                     conn.Open();                      
189                 } finally {  
190                     conn.Close();  
191                 }  
192                 lock (lockThis) {  
193                     count++;  
194                     if (count == testCount) {  
195                         endTime = System.DateTime.Now;  
196                         //Console.Write("Bingo");  
197                         Debug.Write("無 Dispose 且關掉 Pooling: " + (endTime - beginTime) + "\n");  
198                     }  
199                 }  
200             }  
201         }  
202  
203         /// <summary>  
204         /// 用 Using 且關掉 Pooling 且打開  Pooling  
205         /// </summary>  
206         private void UsingPoolingTrueCallback() {  
207             for (int i = 0; i < testTimePerThread; i++) {  
208                 using (SqlConnection conn = new SqlConnection()) {  
209                     conn.ConnectionString = connStr;  
210                     conn.Open();  
211                 }  
212                   
213                 lock (lockThis) {  
214                     count++;  
215                     if (count == testCount) {  
216                         endTime = System.DateTime.Now;  
217                         //Console.Write("Bingo");  
218                         Debug.Write("用 Using 且打開 Pooling: " + (endTime - beginTime) + "\n");  
219                     }  
220                 }  
221             }  
222         }  
223         /// <summary>  
224         /// 用 Using 且關掉 Pooling 且關掉 Pooling  
225         /// </summary>  
226         private void UsingPoolingFalseCallback() {  
227             for (int i = 0; i < testTimePerThread; i++) {  
228                 using (SqlConnection conn = new SqlConnection()) {  
229                     conn.ConnectionString = connStrWithoutPool;  
230                     conn.Open();  
231                 }  
232  
233                 lock (lockThis) {  
234                     count++;  
235                     if (count == testCount) {  
236                         endTime = System.DateTime.Now;  
237                         //Console.Write("Bingo");  
238                         Debug.Write("用 Using 且關掉 Pooling: " + (endTime - beginTime) + "\n");  
239                     }  
240                 }  
241             }  
242         }  
243     }  
244
分享到:
评论

相关推荐

    asp.net sqlconnection con.close和con.dispose区别

    只用dispose是不能关闭connection的,两者不是一回事,只用close也不能释放它所占的内存。 conn.dispose() 是销毁连接,彻底关闭。 您可能感兴趣的文章:C#基础:Dispose()、Close()、Finalize()的区别详解探讨C#中...

    探讨C#中Dispose方法与Close方法的区别详解

    (这里用using或许更好)当我们开发C#代码的时候,经常碰到一个问题,有些class提供Close(),有些class提供Dispose(),那么Dispose和Close到底有什么区别? 在这里,要明确一下C#程序(或者说.NET)中的资源。简单的...

    C#中Dispose和Close的区别

    首先,Dispose和Close基本上应该是一样的。Close是为了那些不熟悉Dispose的开发者设计的。因为基本上所有的developer都知道Close是干吗的(特别是对于那些有C++背景的developer)。

    C#基础:Dispose()、Close()、Finalize()的区别详解

    .net内存回收与Dispose﹐Close﹐Finalize方法一. net的对象使用一般分为三种情况﹕1.创建对象2.使用对象3.释放对象二.创建对象1.创建对象实际分为两个步骤﹕变量类型宣告和初始化对象2.变量类型宣告(declare),如﹕ ...

    DBHelper方便连接数据库 DBHelper

    connection.Close(); connection.Open(); } return connection; } } public static int ExecuteCommand(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd....

    C#中标准Dispose模式的实现

    C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose...

    C#中析构函数、Dispose、Close方法的区别

    本文详细对比了C#中析构函数、Dispose和Close方法的区别,三者都是释放资源,本文介绍了他们各自的使用方法和使用场景,希望对大家有所帮助。

    Delphi new\dispose内存泄露问题解决方案

    delphi dispose释放结构体指针内存泄露解决方案 内存管理 delphi内存泄漏解决方案

    c# dispose用法演示

    c#的dispose用法,相当于c++的析构函数

    c#关闭SQL数据库的两种方式测试

    c#使用Close方法和Dispose方法关闭SQL数据库后再次使用Open方法连接数据库的一个测试

    实现标准的Dispose模式.txt

    实现标准的Dispose模式.txt

    SQLite C# .net 开发所需要的dll文件

    using (SQLiteConnection connection = CreateConnection1()) { DataSet ds = new DataSet(); try { connection.Open(); SQLiteDataAdapter command = new SQLiteDataAdapter(strSQL, connection); command....

    CMS.DBUtility.dll

    using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; ...

    批处理图片/Image Batch Dispose

    图片批处理工具,大小替换,非常不错,jpg ,gif ,png 可以加水印

    extension method to dispose content

    extension method to dispose content

    C# Oracle代理连接,免安装客户端

    using (OracleConnection con = new OracleConnection(OracleConnectionStr)) { //创建数据适配器,将查询语句及连接字符串两个参数传进数据适配器 con.Open(); OracleCommand cmd = con.CreateCommand(); cmd...

    triple_dispose.py

    triple_dispose.py

Global site tag (gtag.js) - Google Analytics