如何解决多次调用存储过程时引发的EF 6超时过期异常?

时间:2015-04-01 21:32:55

标签: c# sql-server entity-framework stored-procedures

我在插入操作期间针对SQL Server数据库运行存储过程。当我尝试在循环中插入多行时,随后执行存储过程时出现以下错误:

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception: The wait operation timed out

On一次尝试,没有抛出异常,但是在列中插入了一个空值,而它应该是存储过程生成的整数。

这是我的存储过程:

ALTER procedure [dbo].[CreateOrRetrieveDataScope] (@dataScopeTypeId int ,@clientId nvarchar(8) ,@areaId int = NULL, @unitId bigint = NULL ,@wheelPosition int = NULL) with recompile as

declare @DataScopeId int

Select_DataScopeId:
    set @DataScopeId = (select Id from DataScopes 
    where DataScopeTypeId = @dataScopeTypeId 
        and ClientId = @clientId 
        and (AreaId is null or (AreaId = @areaId))
        and (UnitId is null or (UnitId = @unitId))
        and (WheelPosition is null or (WheelPosition = @wheelPosition)))

if @DataScopeId is null begin
    begin try
        insert into DataScopes(DataScopeTypeId, ClientId, AreaId, UnitId, WheelPosition) values (@dataScopeTypeId, @clientId, @areaId, @unitId, @wheelPosition);
        goto Select_DataScopeId         
    end try
    begin catch
        goto Select_DataScopeId
    end catch
end 

select @DataScopeId

基本上,这里我们尝试创建一个数据范围id值(如果它不存在)。如果它已经存在,我们只需从proc返回它。我测试了两个(几乎)同时插入的代码,以确保不会有重复。如果已插入,则第二个客户端应始终接收该值。

以下是我的Windows服务中执行它的代码行:

newJob.DataScopeId = db.CreateOrRetrieveDataScope(1, areaMap.Area.ClientID, areaMap.AreaID, null, null).FirstOrDefault().Value;

对数据库中的表中的多行执行存储过程。实质上,新作业是从另一个表中的行创建的。

到目前为止,我已经尝试在执行操作时观察活动监视器,但我无法收集任何特殊情况。我还尝试在SQL Server管理工作室中将连接数设置为0(无限制)。

编辑:再看一下活动监视器,我的存储过程似乎已暂停。

0 个答案:

没有答案