[Reprint].net Database Connection pool

Source: Internet
Author: User

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

    1. Tcp:flags= .... S ....
    2. Tcp:flags= .... A.. S
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.