简体   繁体   English

打开和关闭与SQL Server的连接

[英]Opening and closing connections to SQL Server

I am working in a small team of about a dozen developers on a project being written in C# WPF as the infrastructure/dba. 我正在一个由大约十二名开发人员组成的小团队中工作,使用C#WPF作为基础结构/ dba编写项目。 As I run traces against the SQL Server to see how performance is going, what I am seeing is a constant: 当我对SQL Server运行跟踪以查看性能如何时,我看到的是一个常量:

open connection run some statement close connection exec sp_reset_connection 打开连接运行一些语句关闭连接执行sp_reset_connection

open connection run some statement close connection exec sp_reset_connection 打开连接运行一些语句关闭连接执行sp_reset_connection

open connection run some statement close connection exec sp_reset_connection 打开连接运行一些语句关闭连接执行sp_reset_connection

And so on and on and on. 等等等等。 I have spoke with the devs about this and some have mentioned possible situations where a foreach loop may be encompassing a using statement so a foreach through a datatable would open and close connections throughout the contents of the datatable. 我曾与开发人员讨论过此问题,有些人提到了可能的情况,其中foreach循环可能包含using语句,因此通过数据表的foreach将打开和关闭整个数据表内容的连接。

Question: Is getting better control of the constant opening and closing of connections a worthy goal or are connections really that cheap? 问题:更好地控制连接的持续打开和关闭是一个值得追求的目标,还是连接真的那么便宜? My logic is that while opening and closing a connection may relatively be cheap, nothing is cheap when done in sufficiently large numbers. 我的逻辑是,虽然打开和关闭连接可能相对便宜,但如果进行足够多的连接,则没有什么便宜。

Details: 细节:

  • .Net Framework 4.5.1 .Net Framework 4.5.1
  • SQL Server 2014 SQL Server 2014
  • Entity Framework 6 实体框架6

If you use entity framework, you should create the context just before you need it and dispose it as soon as possible: 如果使用实体框架,则应在需要之前创建上下文并尽快处理它:

using (var someContext = new SomeContext())
{

}

The reason is to avoid memory building up and to avoid thread-safety issues. 原因是为了避免建立内存并避免线程安全问题。

Of course, don't do this in a loop - this is at the level of a request. 当然,不要循环执行此操作-这是在请求级别。

Opening and closing connections to a database are relatively expensive, as can be read in detail here: Performance Considerations (Entity Framework) , but I think the same concept mostly applies without EF. 与数据库的打开和关闭连接相对昂贵,可以在此处进行详细阅读: 性能注意事项(实体框架) ,但是我认为相同的概念在没有EF的情况下也适用。 During loops, it's usually not recommended to open and close the connection every time, but instead open it, process all rows and close the connection. 在循环期间,通常不建议每次都打开和关闭连接,而是打开它,处理所有行并关闭连接。

The answer would be to let the using encompass the loop, instead of the other way around. 答案是让using包含循环,而不是相反。 If performance is relevant (it almost always is), it definately pays to put effort into efficiënt data access, especially early in the development process. 如果性能是相关的(几乎总是如此),则一定要付出努力才能有效地访问数据,尤其是在开发过程的早期。

If performance is an issue, but you don't want to refactor code you should consider setting a ConnectionPooling = true in the connections string. 如果性能是一个问题,但是您不想重构代码,则应考虑在连接字符串中设置ConnectionPooling = true

Connection pooling allows one to keep physical connection, which is generally expensive to setup, while disposing logical connection. 连接池允许在保留逻辑连接的同时保持物理连接(通常设置成本很高)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM