[Reprinted]. net database connection pool and. net database connection
I have previously written an article about database connection pool analysis and analysis, and used code to analyze the role of database connection pool.
Here we need to clarify the concept that the database connection pool is not the content of the database service, but an application architecture created by the client application.
Therefore, we cannot find any option to enable the database connection pool on the database server.
The difference between enabling and not enabling the database connection pool is as follows.
Use Database Connection Pool
Using System; using System. collections. generic; using System. linq; using System. text; using System. data. sqlClient; namespace ConnectionPool {class Program {static void 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 = new SqlConnection (connectionString3) {// open the connection. open (); Console. writeLine ("establish connection 1"); SqlCommand sqlcmd = connection. createCommand (); sqlcmd. commandText = "select @ version;"; SqlDataReader sqlreader = sqlcmd. executeReader ();} using (SqlConnection connection = new SqlConnection (connectionString2) {// open the connection. open (); Console. writeLine ("establish connection 2"); SqlCommand sqlcmd = connection. createCommand (); sqlcmd. commandText = "select @ version;"; SqlDataReader sqlreader = sqlcmd. executeReader ();} using (SqlConnection connection = new SqlConnection (connectionString3) {// open the 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 enabled by default, that is, Pooling = true. The Pooling = false statement must be displayed only when the database connection pool is closed;
The following Network reports are captured through netmon:
We can find that three connections, but there are only two connections in the network. This is because the third connection string is the same as the first one, so the data connection pool is used. We can see that the last two sqlbatchs are consecutive, and there is no TCP three-way handshake between the two sqlbatchs, because the connection pool plays a role.
PS: three-way handshake Error Correction
In the book, I marked TSL: Handshake as a TCP Handshake. This is wrong. The real TCP Handshake should be
Not applicable to database connection pools
Using System; using System. collections. generic; using System. linq; using System. text; using System. data. sqlClient; namespace ConnectionPool {class Program {static void 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 = new SqlConnection (connectionString3) {// open the connection. open (); Console. writeLine ("establish connection 1"); SqlCommand sqlcmd = connection. createCommand (); sqlcmd. commandText = "select @ version;"; SqlDataReader sqlreader = sqlcmd. executeReader ();} using (SqlConnection connection = new SqlConnection (connectionString2) {// open the connection. open (); Console. writeLine ("establish connection 2"); SqlCommand sqlcmd = connection. createCommand (); sqlcmd. commandText = "select @ version;"; SqlDataReader sqlreader = sqlcmd. executeReader ();} using (SqlConnection connection = new SqlConnection (connectionString3) {// open the connection. open (); Console. writeLine ("establish connection 1"); SqlCommand sqlcmd = connection. createCommand (); sqlcmd. commandText = "select @ version;"; SqlDataReader sqlreader = sqlcmd. executeReader ();}}}}
As shown in, there are three database connections, and the three TCP three-way handshake connections are found through the network packet capture by netmon. This proves that the connection pool is not used, and the connection is closed at the end of each connection. Create a new connection again even if the same connection is used next time. Creating a connection consumes resources. If you frequently create and close a connection, performance will be affected.
Reprinted from: http://www.cnblogs.com/xwdreamer/archive/2012/08/07/2626610.html