数据库故障转移期间发生 SqlClient.SqlException

发布于 2024-07-14 22:42:56 字数 210 浏览 2 评论 0原文

我在故障转移场景中设置了两个 Microsoft SQL 2005 数据库。 应用程序连接字符串具有在连接字符串中指定的“故障转移伙伴”。

当当前活动数据库故障转移到从数据库时,用户会在一小段时间内获得 SqlClient.SqlException 并显示消息“现有连接被远程主机强制关闭”。

这主要是由于数据库故障转移的速度还是可以采取其他措施来防止这些错误?

I have two Microsoft SQL 2005 databases setup in a fail over scenario. The application connection strings have the "Failover Partner" specified in the connection string.

When the currently live database fails over to the slave database, there is a small time period that a user can obtain a SqlClient.SqlException with the message "An existing connection was forcibly closed by the remote host".

Is this mainly due to the speed that the databases are failing over or is there something else that can be done to prevent these errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛起拖把扫天下 2024-07-21 22:42:56

您是对的:这与数据库故障转移所需的时间有关。

同步镜像故障转移时间线大致如下:

  1. 主服务器 A 关闭并关闭所有连接。 从此时起,任何尝试连接到服务器 A 的客户端都将被强制关闭其连接。
  2. 主服务器A确保其日志与从服务器B完全同步。
  3. 服务器B的SQL服务启动。
  4. 服务器 B 的 SQL 服务开始接受连接,此时客户端可以再次连接。

无论您的服务器有多快,都会有一个短暂的(至少一秒钟)连接崩溃的情况。 就像 Steven Lowe 所说,你必须捕获该异常,等待,然后重试。 请记住,如果您的应用程序在较大事务中连续发出多个查询,情况会更加复杂 - 您的事务可能会失败。

You're correct: it has to do with how long it takes the databases to fail over.

The synchronous mirroring failover timeline goes something like this:

  1. Primary server A shuts down and closes all connections. From this point forward, any client that tries to connect to server A will get its connection forcibly closed.
  2. Primary server A makes sure its logs are completely synchronized with secondary server B.
  3. Server B's SQL Service starts.
  4. Server B's SQL Service begins accepting connections, and at that point the clients can connect again.

No matter how fast your servers are, there's going to be a brief (at least a second) period when connections will bomb. Like Steven Lowe said, you have to trap that exception, wait, and try again. Keep in mind that it'll be more complex if your app issues several queries in a row as part of a larger transaction - your transaction might fail.

心头的小情儿 2024-07-21 22:42:56

警告:“故障转移合作伙伴”选项几年前不可用(或者至少我不知道!),因此以下解决方案可能已经过时,

我们必须捕获连接丢失异常(几种不同的风格),等等几秒钟,然后重试。 这需要所有数据库操作的包装器来自动执行等待和重试逻辑——这并不是很多工作,而且事实证明相当方便; 我们的工作是检查错误代码和异常类型,并制作一个决策表来确定我们是否可以安全地重试该操作。

caveat: the 'failover partner' option was not available a few years ago (or at least I wasn't aware of it!) so the following solution may be outdated

we had to trap connect-lost exceptions (several different flavors), wait a few seconds, and try again. This necessitated a wrapper for all database operations to automate the wait-and-retry logic - which wasn't a lot of work and proved to be fairly convenient; the effort went into examining the error codes and exception types and making a decision table as to whether we could safely retry the operation or not.

在风中等你 2024-07-21 22:42:56

这是由于您的应用程序尝试使用连接池中的现有连接。 只有发生此错误后,应用程序池才会被清除,并且新连接将使用故障转移框。

This is due to your application attempting to use an existing connection in the connection pool. Only after this error occurs does the application pool get cleared, and new connections use the failover box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文