Details of the use of ADO.NET connection pooling technology

  • 2020-06-07 04:24:37
  • OfStack

Connecting to the database server usually takes a fixed amount of time, and the server also needs a fixed amount of resources to process the connection. Web applications sometimes handle thousands of connections and require considerable resources to handle connections. ADO. NET has the connection pool feature. The function of the connection pool is to reserve a fixed number of connections. When the user connects to the server again using the same connection string, ASO. NET will use connections from the connection pool instead of reinitiating a connection process. When the Close method is called to close the connection, ADO.NET will use the connection from the connection pool instead of reinitiating a connection process. When the Close method is called to close the connection, the connection is returned to the connection pool and the next time the Open method is called, one connection is taken out of the connection pool for use.

The database connection string defaults to connection pooling enabled. The connection pool can be controlled in the connection string using pooling=true/false. The maximum and minimum values of the connection pool can be set using Max Pool Size=200. Min Pool Size = 1; Control.

Create a new console application below to demonstrate the use of connection pooling. In fact, we only use pooling=false/true in Connection String to control. The default is to use the connection pooling technology, i.e. pooling=true:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Threading;
namespace ConsoleApplication1
{
    class Program
    {
        private static readonly string connectionString1 = @"Data Source=.;Initial Catalog=Northwind;Integrated Security=True;pooling=true;";// The default value is true , so you can omit it pooling=true
        private static readonly string connectionString2 = @"Data Source=.;Initial Catalog=Northwind;Integrated Security=True;pooling=false;";
        static void Main(string[] args)
        {
            long startTicks1 = DateTime.Now.Ticks;
            using (SqlConnection conn1 = new SqlConnection(connectionString1))
            {
                for (int i = 0; i < 100; i++)
                {
                    conn1.Open();
                    conn1.Close();
                }
            }
            long endTicks1 = DateTime.Now.Ticks;
            long usedTicks1 = endTicks1 - startTicks1;
            Console.WriteLine("Used time: " + usedTicks1);
            long startTicks2 = DateTime.Now.Ticks;
            using (SqlConnection conn2 = new SqlConnection(connectionString2))
            {
                for (int i = 0; i < 100; i++)
                {
                    conn2.Open();
                    conn2.Close();
                }
            }
            long endTicks2 = DateTime.Now.Ticks;
            long usedTicks2 = endTicks2 - startTicks2;
            Console.WriteLine("Used time: " + usedTicks2);
        }
 
    }
}

Output results:

pooling=true: 710040
pooling=false: 3100177


Related articles: