When I had nothing to do at night, I suddenly wanted to test the connection speed improvement caused by the ADO. net connection pool. I wrote the following:Code:
1 Using System;
2 Using System. configuration;
3 Using System. Data. sqlclient;
4 Using System. diagnostics;
5
6 Namespace Leleapplication1
7 {
8 Class Program
9 {
10 Static Void Main ( String [] ARGs)
11 {
12 String _ Connstring = Configurationmanager. connectionstrings [ " Connstr " ]. Tostring ();
13
14 Sqlconnection Conn = New Sqlconnection (_ connstring );
15
16 Stopwatch SW = New Stopwatch ();
17
18 Sw. Start ();
19 Conn. open ();
20 Sw. Stop ();
21 Console. writeline ( " 1 connection time: {0} " , SW. elapsedticks. tostring ());
22
23 Conn. Close (); // Close connection
24
25 Sw. Reset ();
26 Sw. Start ();
27 Conn. open (); // The connection is directly allocated from the connection pool, so the speed is much faster.
28 Sw. Stop ();
29
30 Console. writeline ( " 2 connection time: {0} " , SW. elapsedticks. tostring ());
31
32 Conn. Dispose (); // Release connection
33
34 Conn. connectionstring = _ Connstring;
35
36 Sw. Reset ();
37 Sw. Start ();
38 Conn. open (); // After dispose (), you can still connect! (The premise is that you must reset the connection string)
39 Sw. Stop ();
40 Console. writeline ( " 3 connection time: {0} " , SW. elapsedticks. tostring ()); // From the output result, this time is still much faster than the first connection. It seems that it is also obtained from the connection pool.
41
42 Conn. Close ();
43
44
45 Using (Sqlconnection conn2 = New Sqlconnection (_ connstring ))
46 {
47 Try
48 {
49 Sw. Reset ();
50 Sw. Start ();
51 Conn2.open (); // Even if you create a completely different connection object, as long as the connection string is the same, existing connections will still be allocated from the connection pool, so the speed is still very fast.
52 Sw. Stop ();
53 Console. writeline ( " 4 connection time: {0} " , SW. elapsedticks. tostring ());
54 }
55 Catch (Exception)
56 {
57
58Throw;
59}
60 Finally {
61Conn2.close ();
62}
63 }
64
65
66
67 // If you call close () or dispose () multiple times in a row, no exception is thrown, but if you call open () multiple times in a row, an exception is thrown.
68 // Conn. Close ();
69 // Conn. Close ();
70
71 // Conn. Dispose ();
72 // Conn. Dispose ();
73 Console. Read ();
74
75 // Even if the connection is not closed, the connection is automatically destroyed when the program exits.
76 }
77 }
78 }
Local execution result 1:
--------------------------------
1 time used for connection: 480219
2 time used for connection: 130
3 connection time: 60
4 connection time: 47
--------------------------------
Local execution result 2:
--------------------------------
1 time used for connection: 476064
2 time used for connection: 137
3 time used for connection: 1411
4 connection time: 49
--------------------------------
Local execution result 3:
--------------------------------
1 time used for connection: 691719
2 time used for connection: 132
3 connection time: 69
4 connection time: 53
--------------------------------
It can be seen that the connection pool technology has indeed increased the speed of opening the connection object, but it is somewhat surprising that "4 connections" are always much faster than "2 connections? Since they are all allocated from the connection pool, the speed is almost the same! In addition, after dispose (), the re-connection speed is sometimes fast, sometimes slow (but generally faster than the first connection). I don't know if it is a problem with my machine.