.net 连接池
我不明白常规连接和连接池之间的语法差异是什么。
当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在此处阅读有关连接池的信息< /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.
您不使用连接来控制连接池,而是使用连接字符串来控制连接池。大多数 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 theDispose
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 callConnection.Close()
as soon as possible. Especially if your application is under some form of load.像上面一样,使用 SqlConnection 将连接池的管理从您手中抽象出来。默认情况下,ADO.NET 连接池是打开的,您可以进一步控制它,例如关闭它或控制连接字符串中的池大小,例如
关闭
或控制最小和最大
更多详细信息解释和验证池的方法
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
or controlling min and max
More details explanation and way to verify the pool
http://www.codeproject.com/KB/dotnet/ADONET_ConnectionPooling.aspx
据我所知,
连接池由 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.
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.