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 }