.net下数据库连接池

之前写过一篇博客数据库连接池分析 分析了,通过代码来分析数据库连接池的作用。
这里要搞清楚一个概念,数据库连接池并不是数据库服务的内容,而是客户端应用程序创建的一种应用架构。
所以我们在数据库服务器上找不到什么开启数据库连接池的选项。
接下来将开启与不开启数据库连接池的区别。

使用数据库连接池

双击代码全选

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

View Code 

    

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))

            {

                //打开连接

                connection.Open();

                Console.WriteLine("建立连接1");

    

                SqlCommand sqlcmd = connection.CreateCommand();

                sqlcmd.CommandText = "select @@version;";

                SqlDataReader sqlreader = sqlcmd.ExecuteReader();

            }

    

            using (SqlConnection connection = new SqlConnection(connectionString2))

            {

                //打开连接

                connection.Open();

                Console.WriteLine("建立连接2");

    

                SqlCommand sqlcmd = connection.CreateCommand();

                sqlcmd.CommandText = "select @@version;";

                SqlDataReader sqlreader = sqlcmd.ExecuteReader();

            }

    

            using (SqlConnection connection = new SqlConnection(connectionString3))

            {

                //打开连接

                connection.Open();

                Console.WriteLine("建立连接1");

    

                SqlCommand sqlcmd = connection.CreateCommand();

                sqlcmd.CommandText = "select @@version;";

                SqlDataReader sqlreader = sqlcmd.ExecuteReader();

            }

    

        }

    }

}

 

在connectiongString中数据库连接池默认就是开启的,也就是Pooling=true;只有要关闭数据库连接池的时候,才需要显示声明Pooling=false;
通过netmon抓取网络报如下

通过上图我们可以发现,三次连接,但是网络中只有两次连接,这是因为第三次连接字符串跟第一次一样,所以使用了数据连接池。我们可以看到最后的两次SQLBatch是连续的,两次SQLBatch之间没有TCP三次握手连接,因为连接池起了作用。

不适用数据库连接池

双击代码全选

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

View Code 

    

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))

            {

                //打开连接

                connection.Open();

                Console.WriteLine("建立连接1");

    

                SqlCommand sqlcmd = connection.CreateCommand();

                sqlcmd.CommandText = "select @@version;";

                SqlDataReader sqlreader = sqlcmd.ExecuteReader();

            }

    

            using (SqlConnection connection = new SqlConnection(connectionString2))

            {

                //打开连接

                connection.Open();

                Console.WriteLine("建立连接2");

    

                SqlCommand sqlcmd = connection.CreateCommand();

                sqlcmd.CommandText = "select @@version;";

                SqlDataReader sqlreader = sqlcmd.ExecuteReader();

            }

    

            using (SqlConnection connection = new SqlConnection(connectionString3))

            {

                //打开连接

                connection.Open();

                Console.WriteLine("建立连接1");

    

                SqlCommand sqlcmd = connection.CreateCommand();

                sqlcmd.CommandText = "select @@version;";

                SqlDataReader sqlreader = sqlcmd.ExecuteReader();

            }

    

        }

    }

}

 


如上图所示,有三次数据库连接,然后通过netmon抓取网络包发现又3次的TCP三次握手连接,这证明是没有使用连接池,每一次连接结束都会关闭连接。下次即使使用相同的连接也要重现新建连接。创建连接比较消耗资源,如果频繁地创建关闭连接会影响性能。
作者:xwdreamer
出处:http://www.cnblogs.com/xwdreamer

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Grow your business fast with

Suku