简体   繁体   English

我应该在每次使用它时创建一个全新的SqlConnection,还是每次尝试重新打开一个现有的连接?

[英]Should I create a brand new SqlConnection each time I want to use it, or just attempt to re-open an existing connection each time?

I have a long-running .NET process (a Windows service), which talks to a SQL Server. 我有一个长期运行的.NET进程(Windows服务),它与SQL Server通讯。

Originally, I opened a connection when the service started up, and just kept it open. 最初,我在服务启动时打开了一个连接,并保持打开状态。 However, occasionally a network hiccup or reboot of the SQL Server would break that connection, and my process wouldn't know this. 但是,偶尔出现网络故障或SQL Server重新启动会中断该连接,而我的进程也不知道这一点。 It would attempt to keep throwing SQL against a closed connection. 它将尝试对关闭的连接不断抛出SQL。

Trying to maintain an open connection like that was clearly me trying to be too clever. 试图保持像这样的开放连接显然是我试图变得太聪明。 So, do I: 我也是:

  1. Create (and open) a new SqlConnection object every time 每次创建(并打开)一个新的SqlConnection对象
  2. Create the connection once on service start-up, and just attempt to re-open the connection every time 在服务启动时创建一次连接,然后每次尝试重新打开连接

For the latter (#2), I have this code: 对于后者(#2),我有以下代码:

if(connection.State != ConnectionState.Open)
{
  connection.Open();
}

I only have to do this because the connection already exists. 我只需要这样做,因为连接已经存在。 If I created the connection fresh each time (#1), clearly it would be closed and I would need to open it. 如果我每次都重新创建连接(#1),那么显然它将关闭并且需要打开它。

What I'm hoping to do is take advantage of SQL connection pooling to actually not open a new connection every time, but just let the connection pool manage that -- give me an open connection when it has one, or open a new one when it doesn't. 我希望做的是利用SQL连接池来实际上不是每次都打开一个新连接,而只是让连接池来管理它-当有一个连接时给我一个打开的连接,或者在有一个连接时给我一个打开的连接。没有。

To achieve this, does it matter if I create the connection fresh each time (#1), or if I just-reuse a connection and attempt to re-open it each time (#2)? 为此,是否每次都重新创建连接(#1)还是重新使用连接并每次尝试重新打开连接(#2)都重要吗?

Connection pooling means that even if you do (1), under the hood, the framework will do (2) for you for improved performance. 连接池意味着即使在后台执行(1),框架也将为您执行(2),以提高性能。 So you can feel free to create a new connection each time. 因此,您可以随时创建一个新的连接。

It's worth pointing out that, for the most part, the pooling only applies to identical connection strings. 值得指出的是,在大多数情况下,池化仅适用于相同的连接字符串。 So if you change certain options, those connections may not be pooled. 因此,如果更改某些选项,则可能不会合并这些连接。

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

相关问题 脚手架每次使用都会创建一个新的DbSet - Scaffolding create a new DbSet for each time i use it 每次在C#中收到新连接时,如何创建新线程? - How do I create a new thread each time a new connection is received in C#? 每次打开页面时使用新的ViewModel - Using new ViewModel each time I open a page 每次使用后,我应该创建新的BinaryReader / BinaryWriter吗? - Should I create new BinaryReader/BinaryWriter after each use? 我应该保持SqlConnection打开吗? - Should I keep the SqlConnection open? 每次您要使用类中的函数时,都调用“ new” - Calling 'new' each time you want to use a function from a class 我想在每次调用此方法时都保持totalNumberOfRecords - i want to maintain the totalNumberOfRecords in this while each time i call this method 一遍又一遍地计算时间后重新打开表格 - Re-Open a Form after done Counting time over and over 每次下载文件时,WebClient都会打开一个新连接,并且所有连接都保持建立状态 - WebClient is opening a new connection each time I download a file and all of them stay established 如果我想每次都更改Referer header,就不能使用GetStringAsync()? - Can't use GetStringAsync() if I want to change the Referer header each time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM