SaveChanges上的实体框架间歇超时

时间:2015-05-09 17:54:13

标签: sql-server entity-framework

我们在使用Entity Framework调用SaveChanges时遇到间歇性超时,我决定打开Database.Log上的DbContext来尝试找出正在发生的事情,输出结果是如下:

Opened connection at 5/9/2015 4:56:57 PM +00:00

Started transaction at 5/9/2015 4:56:57 PM +00:00

UPDATE [Search].[IndexingStatuses]
SET [DateTimeLastUpdated] = @0, [IndexStores_IndexStoreId] = @1
WHERE ([CustomerId] = @2)

-- @0: '5/9/2015 4:03:57 PM' (Type = DateTime2)

-- @1: '2' (Type = Int16)

-- @2: '1' (Type = Int32)

-- Executing at 5/9/2015 4:56:57 PM +00:00

-- Completed in 1 ms with result: 1

Failed to commit transaction at 5/9/2015 4:57:13 PM +00:00 with error: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

Closed connection at 5/9/2015 4:57:13 PM +00:00

这很奇怪,这是一个简单的更新,你可以看到事务在4:56:57开始,更新需要1毫秒,但由于某种原因,在4:57:13有一个超时,这只是16秒后,无论如何都低于我们的超时限制。

有谁知道为什么我们间歇地得到这些?

1 个答案:

答案 0 :(得分:0)

我不能肯定地说,但似乎你的服务器运行的内存非常少。以下其中一项可能对您有用 -

  1. Incresing Timeout一次获得更大的值。可能查询在第二次运行时不会花费那么多时间,因为它已经有执行计划。
  2. 删除事务日志,tempDb数据。

  3. 在查询中使用过滤条件,一次更新较小的数据集。