简体   繁体   English

在客户端的SQL Server 2012中检测断开的连接

[英]Detect broken connection in SQL Server 2012 on the client side

We are developing a C# program for connecting with a database and sending some packets to the database, then storing the data into the database. 我们正在开发一个C#程序,用于与数据库连接并将一些数据包发送到数据库,然后将数据存储到数据库中。 C# program and SQL Server are on two separate computers, and we want to ensure that connection between C# program and SQL Server is always established. C#程序和SQL Server位于两台单独的计算机上,我们希望确保始终在C#程序和SQL Server之间建立连接。 If this connection is broken, we must try again to connect to SQL Server. 如果此连接断开,则必须再次尝试连接到SQL Server。

But we have some problems: 但是我们有一些问题:

  1. After establishing connection, if we stop SQL Server, the client side (C# program) is not notified of the closed connection. 建立连接后,如果我们停止SQL Server,则不会通知客户端(C#程序)已关闭的连接。 After sending a packet from client to SQL Server, we detect broken connection. 从客户端向SQL Server发送数据包后,我们检测到断开的连接。 Note : we don't like to loose any packets 注意 :我们不希望丢失任何数据包

  2. If we unplug the Ethernet cable between the two computers, the client side will notice the broken connection after sending a packet, after approximately 20 seconds, and in this interval we may loose several packets. 如果我们拔掉两台计算机之间的以太网电缆,则客户端将在发送数据包后大约20秒后注意到断开的连接,在此间隔内,我们可能会丢失几个数据包。

Is there quicker and reliable methods to detect broken/closed connection in SQL Server? 是否有更快,更可靠的方法来检测SQL Server中的断开/关闭连接?

Read through this article. 通读这篇文章。 It should give yo u enough info about how you should be implementing your solution. 它应该为您提供有关应如何实施解决方案的足够信息。 https://blogs.msdn.microsoft.com/cesardelatorre/2017/03/26/using-resilient-entity-framework-core-sql-connections-and-transactions-retries-with-exponential-backoff/ https://blogs.msdn.microsoft.com/cesardelatorre/2017/03/26/using-resilient-entity-framework-core-sql-connections-and-transactions-retries-with-exponential-backoff/

Edits: In regards with not wanting to loose packets, it completely depends on your implementation. 编辑:关于不想丢失数据包,这完全取决于您的实现。 If there is an exception you might want to persist the data and retry sending the data when connection is back up again. 如果发生异常,您可能希望保留数据并在再次备份连接时重试发送数据。

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

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