处理连接或关闭连接 [英] Dispose the connection or Close the connection

查看:160
本文介绍了处理连接或关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两种方法中哪一种方法具有更好的性能?

  using(var DB_Connection_s = new DBConnection())
{
// todo:与数据库连接交互
}

只是:

  DB_Connection_s.Close(); 

结尾。



第一种方法使得池化概念无用?因为如果我在每次使用时处理连接,那么我每次都必须打开一个新的连接(并且在池中不会有任何连接)。

解决方案

当您在Connection ...


上调用Close或Dispose时,连接释放回池中

source = SQL Server连接池(ADO.NET)



因此,删除任何由于遗漏的池连接造成的性能损失的担心。

从代码立场上的差异应该是这么小,以至于使用语句应该总是使用


Which one of the following two methods has better performance ?

using( var DB_Connection_s = new DBConnection() )
{
 //todo: interact with database connection
}

or just :

DB_Connection_s.Close();

at the end.

Does the first method make the pooling concept useless? Because if I dispose the connection with each use, then I have to open a new connection every time (and there won't be any connections in the pool).

解决方案

Connections are released back into the pool when you call Close or Dispose on the Connection..."

source = SQL Server Connection Pooling (ADO.NET)

So, remove any worry about performance loss caused by missed pooled connections.
From the code standpoint the difference should be so minimal that the using statement should always be used

这篇关于处理连接或关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆