Before I wrote a blog database connection pool analysis and analysis, through the code to analyze the role of database connection pool.
The idea here is that the database connection pool is not the content of the database service, but rather an application architecture created by the client application.
So we can't find anything on the database server that opens the database connection pool option.
Next, the difference between opening and not opening the database connection pool is turned on.
Using a database connection pool
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Data.SqlClient;Namespaceconnectionpool{Classprogram {Staticvoid Main (String[] args) {String connectionString3 ="Data source=jackal-pc;initial catalog=xwdb; User Id=xuwei; password=1234;";String connectionString2 ="Data source=jackal-pc;initial catalog=xwdb; User Id=xuwei2; Password=xuwei2;";using (SqlConnection connection =NewSqlConnection (ConnectionString3)) {//Open connectionConnection. Open (); Console.WriteLine ("Establishing a Connection 1"); SqlCommand sqlcmd =Connection. CreateCommand (); Sqlcmd.commandtext ="SELECT @ @version;"; SqlDataReader Sqlreader =sqlcmd. ExecuteReader (); }using (SqlConnection connection =NewSqlConnection (connectionString2)) {//Open connectionConnection. Open (); Console.WriteLine ("Establishing a Connection 2"); SqlCommand sqlcmd =Connection. CreateCommand (); Sqlcmd.commandtext ="SELECT @ @version;" sqlcmd. ExecuteReader (); } using (SqlConnection connection = new SqlConnection (connectionString3) {// open connection connection. Open (); Console.WriteLine ( " Establish connection 1 "); SqlCommand sqlcmd = connection. CreateCommand (); Sqlcmd.commandtext = "select @ @version; "; SqlDataReader sqlreader = sqlcmd. ExecuteReader (); } } }}
In connectiongstring, the database connection pool is turned on by default, that is, pooling=true; only when you close the database connection pool, you need to display the declaration pooling=false;
Crawl through the Netmon Web report as follows
We can find that three connections, but only two connections in the network, because the third connection string is the same as the first time, so the data connection pool is used. We can see that the last two times the sqlbatch is contiguous, two times sqlbatch there is no TCP three handshake connection, because the connection pool is functioning.
PS: Three-time handshake error correction
In the book I marked Tsl:handshake for the TCP handshake, which is wrong, the real TCP handshake should be
- Tcp:flags= .... S ....
- Tcp:flags= .... A.. S
- Tcp:flags= .... A....
Database connection pool Not applicable
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Data.SqlClient;Namespaceconnectionpool{Classprogram {Staticvoid Main (String[] args) {String connectionString3 ="Data source=jackal-pc;initial catalog=xwdb; User Id=xuwei; password=1234; Pooling=false;";String connectionString2 ="Data source=jackal-pc;initial catalog=xwdb; User Id=xuwei2; Password=xuwei2; Pooling=false;";using (SqlConnection connection =NewSqlConnection (ConnectionString3)) {//Open connectionConnection. Open (); Console.WriteLine ("Establishing a Connection 1"); SqlCommand sqlcmd =Connection. CreateCommand (); Sqlcmd.commandtext ="SELECT @ @version;"; SqlDataReader Sqlreader =sqlcmd. ExecuteReader (); }using (SqlConnection connection =NewSqlConnection (connectionString2)) {//Open connectionConnection. Open (); Console.WriteLine ("Establishing a Connection 2"); SqlCommand sqlcmd =Connection. CreateCommand (); Sqlcmd.commandtext ="SELECT @ @version;" sqlcmd. ExecuteReader (); } using (SqlConnection connection = new SqlConnection (connectionString3) {// open connection connection. Open (); Console.WriteLine ( " Establish connection 1 "); SqlCommand sqlcmd = connection. CreateCommand (); Sqlcmd.commandtext = "select @ @version; "; SqlDataReader sqlreader = sqlcmd. ExecuteReader (); } } }}
As shown, there are three database connections and then 3 TCP three handshake connections through the Netmon Crawl network packet, which proves that the connection pool is not used and the connection is closed at the end of each connection. The next time you want to reproduce the new connection, even if you use the same connection. Creating a connection consumes resources that can affect performance if you create close connections frequently.
Reprinted from: http://www.cnblogs.com/xwdreamer/archive/2012/08/07/2626610.html
[Reprint].net Database Connection pool