SqlException超时未达到

发布于 2024-11-03 06:47:27 字数 555 浏览 1 评论 0原文

我们的服务器有时会抛出这个众所周知的异常:

超时已过期。操作完成之前超时时间已过,或者服务器没有响应。

当服务器处理大请求时,会在压力下发生这种情况。 我做了一些研究,发现我可以更改连接字符串连接超时设置和/或SqlCommand.Timeout数据读取器属性。

默认情况下,sql命令超时设置为30秒,连接超时设置为15,我们从不覆盖它们。

我重现了上下文并在管理工作室中手动执行了失败的请求。 它们的持续时间大约 1 秒,并且总是远远超过 30

但奇怪的是,当我查看服务器日志时,请求调用立即抛出此异常。 我的意思是,请求正在执行,一毫秒后引发异常。 对不起,让我用极客的眼光来看看这个8-o

为了完整起见,我们的 SQL 实例以同步模式与另一个实例进行镜像。 我们通过表适配器使用 Ado.Net。

From time to time our server throw this well-known exception:

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

This happens under pressure when the server is working on big requests.
I did some research and found out that I could change connection string connection timeout setting and/or SqlCommand.Timeout data reader properties.

By default, sql command timeout is set to 30 seconds and connection timeout to 15, and we never override them.

I reproduced the context and executed the failling requests by hand in management studio.
Their duration are around 1 second and always far beyond 30.

But strangely when I take a look at the server logs, this exception is thrown right away the request call.
I mean, the request is executing and one millisecond later the exception is raised.
Excuse me but let me do my geek look about this 8-o.

To be complete, our sql instance is mirrored with another one in synchronous mode.
We use Ado.Net through table adapters.

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

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

发布评论

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

评论(3

病女 2024-11-10 06:47:27

事实上,即使设置了 READ_COMMITED_SNAPSHOT 之后,我们仍然遇到这些随机超时。

将镜像设置为异步模式并没有帮助,在多个线程中完成的查询仍然在大约 1 毫秒后随机超时,总是在繁忙时段。另一方面,触发超时的特定查询(INSERT 语句)本身执行速度非常快(CPU 时间不到 1 毫秒,平均读取次数约为 10 次)。

调用堆栈如下:

在 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)

在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)

在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection 外连接,DbConnectionFactory 连接工厂)

在 System.Data.SqlClient.SqlConnection.Open()

因此超时似乎与查询本身无关。

根据另一篇文章: 多线程 Windows 中的多个同时 SQL 连接超时服务和链接的MSDN 博客文章 谈论 ADO.NET 错误,我们尝试在连接字符串中将连接超时设置为150

无法确定我们是否遇到了此错误,但自此更改以来没有再引发超时。

In fact we still experienced these random timeouts even after READ_COMMITED_SNAPSHOT was set.

Setting the mirror in asynchronous mode didn't helped, queries done in multiple threads still randomly timeouted after about 1ms, always on busy periods. On the other hand the specific query that triggered the timeout (an INSERT statement) executed itself really fast (less than 1ms CPU and about 10 reads in average).

The call stack was the following:

at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)

at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)

at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)

at System.Data.SqlClient.SqlConnection.Open()

So the timeout didn't seem related to the query itself.

According to this other post : Multiple Simultaneous SQL Connection Timeouts In Multithreaded Windows Service and the linked MSDN blog post speaking about an ADO.NET bug, we tried to set the connection timeout to 150 in the connection string.

Can't be certain we were experiencing this bug, but no more timeouts have been thrown since this change.

找个人就嫁了吧 2024-11-10 06:47:27

最后,经过几个小时的跟踪和分析,问题在于两件事的相关性:

  1. 读取已提交的默认 Sql Server 隔离级别导致阻塞情况
  2. 一些调整不当的请求和与无索引表混合的存储过程

已修复

ALTER DATABASE <dbName> SET READ_COMMITTED_SNAPSHOT ON

第一个原因 第二个是清晰的请求重写和表索引。

In the end, after hours of tracking and profiling, the issue was the correlation of two things:

  1. Read committed default Sql Server isolation level leading to blocking situations
  2. Some really badly tuned requests and stored procedures mixed with indexless tables

The first cause was fixed with

ALTER DATABASE <dbName> SET READ_COMMITTED_SNAPSHOT ON

The second with lucid requests rewriting and table indexing.

追星践月 2024-11-10 06:47:27

此时我将运行 SQL Profiler 并查看正在执行哪些查询。

I would run SQL Profiler at this point and see what queries are being executed.

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