连接池的最大连接数对并发数的影响

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public class ConnStr
    {
        public string Flag { get; set; }

        public string ConnString { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string connStringNoPooling = @"data source=(local)\sqlserver2014;initial catalog=master;Integrated Security=True;pooling=false;";
            string connStringDefault = @"data source=(local)\sqlserver2014;initial catalog=master;Integrated Security=True;";
            string connStringPooling100 = @"data source=(local)\sqlserver2014;initial catalog=master;Integrated Security=True;pooling=true;connection lifetime=10;min pool size = 100;max pool size=100; ";
            string connStringPooling200 = @"data source=(local)\sqlserver2014;initial catalog=master;Integrated Security=True;pooling=true;connection lifetime=10;min pool size = 100;max pool size=200; ";
            string connStringPooling300 = @"data source=(local)\sqlserver2014;initial catalog=master;Integrated Security=True;pooling=true;connection lifetime=10;min pool size = 100;max pool size=300; ";
            string connStringPooling500 = @"data source=(local)\sqlserver2014;initial catalog=master;Integrated Security=True;pooling=true;connection lifetime=10;min pool size = 100;max pool size=500; ";
            string connStringPooling1000 = @"data source=(local)\sqlserver2014;initial catalog=master;Integrated Security=True;pooling=true;connection lifetime=10;min pool size = 100;max pool size=1000; ";

            List<ConnStr> list = new List<ConnStr>() {
                 new ConnStr(){ Flag="无连接池",ConnString=connStringNoPooling }
                ,new ConnStr(){ Flag="默认    ",ConnString=connStringDefault }
                ,new ConnStr(){ Flag="最大100 ",ConnString=connStringPooling100 }
                ,new ConnStr(){ Flag="最大200 ",ConnString=connStringPooling200 }
                ,new ConnStr(){ Flag="最大300 ",ConnString=connStringPooling300 }
                ,new ConnStr(){ Flag="最大500 ",ConnString=connStringPooling500 }
                ,new ConnStr(){ Flag="最大1000",ConnString=connStringPooling1000 }
            };

            foreach (ConnStr connObj in list)
            {
                Stopwatch sw = Stopwatch.StartNew();
                Parallel.For(0, 1000, item =>
                {
                    TestQuery(connObj.ConnString);
                });
                sw.Stop();
                Console.WriteLine("{0} => 消耗毫秒数:{1}", connObj.Flag, sw.ElapsedMilliseconds);
            }

            Console.Read();
        }

        private static void TestQuery(string connString)
        {
            string sql = "select top 1 * from dbo.spt_values;WAITFOR DELAY '00:00:00.500'";
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
            }
        }
    }
}


如此看来, 连接数增加对增加并发有一定的改善作用,但达到一定程度后不再上升。可能 256 ~ 512 比较合适。

可能跟CPU性能也有关系, 哪位有比较强悍的机器可以一试。


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值