.net 连接池

发布于 2024-10-21 03:48:23 字数 245 浏览 0 评论 0原文

我不明白常规连接和连接池之间的语法差异是什么。

当我使用 using 键时,例如:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    command.ExecuteNonQuery();
}

这是执行连接池的方式吗?

I don't get what is the syntax difference between regular connection and connection pool.

When I'm using the using key such as:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    command.ExecuteNonQuery();
}

Is this the way to perform a connection pool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫竹語嫣☆ 2024-10-28 03:48:23

您可以在此处阅读有关连接池的信息< /a>.

基本上,只要连接字符串相同(包括大小写),连接就会从同一个连接池中获取。

You can read about connection pooling here.

Basically, as long as the connection string is the same (including case), connections will be taken from the same connection pool.

三生路 2024-10-28 03:48:23

您不使用连接来控制连接池,而是使用连接字符串来控制连接池。大多数 ADO 提供程序默认使用池化。

using 语句用于调用对象(在本例中为连接类)的 Dispose 方法。通过这样做,连接要么返回到池中,要么被断开连接,具体取决于连接字符串配置。

您还应该注意,如果使用分布式事务(.Net 4 中的 TransactionScope),连接不会直接返回到池中。当事务完成/回滚时,连接将被返回。

如果您不使用 using,您应该确保一旦调用 Connection.Close()可能的。特别是当您的应用程序承受某种形式的负载时。

You do not control the connection pool with connections but with the connection string. Most ADO providers use pooling per default.

The using statement is used to call the Dispose method of the object (in this case the connection class). By doing so, the connection is either returned to the pool or being disconnected depending of the connection string configuration.

You should also be aware of that connections are not returned to the pool directly if distributed transactions are being used (TransactionScope in .Net 4). The connections are returned when the transaction have been completed/rolled back.

If you are not using using, you should make sure that you call Connection.Close() as soon as possible. Especially if your application is under some form of load.

笑看君怀她人 2024-10-28 03:48:23

像上面一样,使用 SqlConnection 将连接池的管理从您手中抽象出来。默认情况下,ADO.NET 连接池是打开的,您可以进一步控制它,例如关闭它或控制连接字符串中的池大小,例如

关闭

Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Pooling=false;

或控制最小和最大

Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Min Pool Size=5; Max Pool Size=20;

更多详细信息解释和验证池的方法
http://www.codeproject.com/KB/dotnet/ADONET_ConnectionPooling.aspx

The management of connection pool is abstracted away from you using SqlConnection like in the above. By default in ADO.NET connection pool is turn on and you can further control that such as turning it off or controlling the pool size in the connection string e.g.

Turn off

Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Pooling=false;

or controlling min and max

Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Min Pool Size=5; Max Pool Size=20;

More details explanation and way to verify the pool
http://www.codeproject.com/KB/dotnet/ADONET_ConnectionPooling.aspx

梦回旧景 2024-10-28 03:48:23

据我所知,

连接池由 ado.net 客户端管理,因为与数据库建立连接是昂贵的操作。 ado.net 建立连接池,每当您需要连接时,它都会尝试从池中提供连接。即使您对客户端代码说关闭连接,ado.net 也会保留该连接以供以后使用。 应用程序的 web.config 文件中告诉您不管理池

连接池。当您使用 using 语句时,您告诉该对象应该在 using 结束时被释放。

as far as i know,

connection pooling is managed by ado.net client, since making connections to db is costly operation. ado.net makes pool of connections and whenever you need a connection it tries to give it from pool. even if you say to client code to close connection, ado.net hold that connection for later use. you dont manage the pooling

connection pool is told in web.config file of application. when you use using statements , you tell that object should be disposed end of using.

定格我的天空 2024-10-28 03:48:23

SQL 连接默认是连接池。
(最大池大小=100)

您可以从连接字符串配置连接池。

您可以从此处<找到有关连接字符串的更多信息< /a>.

The SQL connection defaul is connection pooling.
(Max Pool Size=100)

You can config you connection pool from connection string.

You can find more informamtion about connection string from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文