现在的位置: 首页 > 编程语言 > 正文

.net下数据库连接池开启与不开启的区别有哪些

2020年06月03日 编程语言 ⁄ 共 2957字 ⁄ 字号 评论关闭

  数据库连接池并不是数据库服务的内容,而是客户端应用程序创建的一种应用架构。所以我们在数据库服务器上找不到什么开启数据库连接池的选项。下面学步园小编来讲解下.net下数据库连接池开启与不开启的区别有哪些?

  .net下数据库连接池开启与不开启的区别有哪些

  使用数据库连接池

  ViewCode

  usingSystem;

  usingSystem.Collections.Generic;

  usingSystem.Linq;

  usingSystem.Text;

  usingSystem.Data.SqlClient;

  namespaceConnectionPool

  {

  classProgram

  {

  staticvoidMain(string[]args)

  {

  stringconnectionString3="DataSource=jackal-pc;InitialCatalog=xwdb;UserId=xuwei;Password=1234;";

  stringconnectionString2="DataSource=jackal-pc;InitialCatalog=xwdb;UserId=xuwei2;Password=xuwei2;";

  using(SqlConnectionconnection=newSqlConnection(connectionString3))

  {

  //打开连接

  connection.Open();

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

  SqlCommandsqlcmd=connection.CreateCommand();

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

  SqlDataReadersqlreader=sqlcmd.ExecuteReader();

  }

  using(SqlConnectionconnection=newSqlConnection(connectionString2))

  {

  //打开连接

  connection.Open();

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

  SqlCommandsqlcmd=connection.CreateCommand();

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

  SqlDataReadersqlreader=sqlcmd.ExecuteReader();

  }

  using(SqlConnectionconnection=newSqlConnection(connectionString3))

  {

  //打开连接

  connection.Open();

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

  SqlCommandsqlcmd=connection.CreateCommand();

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

  SqlDataReadersqlreader=sqlcmd.ExecuteReader();

  }

  }

  }

  }

  在connectiongString中数据库连接池默认就是开启的,也就是Pooling=true;只有要关闭数据库连接池的时候,才需要显示声明Pooling=false;

  所以使用了数据连接池。我们可以看到最后的两次SQLBatch是连续的,两次SQLBatch之间没有TCP三次握手连接,因为连接池起了作用。

  .net下数据库连接池开启与不开启的区别有哪些

  不适用数据库连接池

  ViewCode

  usingSystem;

  usingSystem.Collections.Generic;

  usingSystem.Linq;

  usingSystem.Text;

  usingSystem.Data.SqlClient;

  namespaceConnectionPool

  {

  classProgram

  {

  staticvoidMain(string[]args)

  {

  stringconnectionString3="DataSource=jackal-pc;InitialCatalog=xwdb;UserId=xuwei;Password=1234;Pooling=false;";

  stringconnectionString2="DataSource=jackal-pc;InitialCatalog=xwdb;UserId=xuwei2;Password=xuwei2;Pooling=false;";

  using(SqlConnectionconnection=newSqlConnection(connectionString3))

  {

  //打开连接

  connection.Open();

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

  SqlCommandsqlcmd=connection.CreateCommand();

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

  SqlDataReadersqlreader=sqlcmd.ExecuteReader();

  }

  using(SqlConnectionconnection=newSqlConnection(connectionString2))

  {

  //打开连接

  connection.Open();

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

  SqlCommandsqlcmd=connection.CreateCommand();

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

  SqlDataReadersqlreader=sqlcmd.ExecuteReader();

  }

  using(SqlConnectionconnection=newSqlConnection(connectionString3))

  {

  //打开连接

  connection.Open();

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

  SqlCommandsqlcmd=connection.CreateCommand();

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

  SqlDataReadersqlreader=sqlcmd.ExecuteReader();

  }

  }

  }

  }

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

  以上就是关于“.net下数据库连接池开启与不开启的区别有哪些”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!

抱歉!评论已关闭.