I am running a stored procedure against a SQL server database during an insert operation. When I attempt to insert multiple rows within a loop, I get the following error when the stored procedure is subsequently executed:
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 One attempt, the exception was not thrown, but a null value was inserted into the column whereas it should have been an integer generated by the stored procedure.
Here is my stored procedure:
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
Basically, here we attempt to create a data scope id value if it doesn't exist. If it already exists we just return it from the proc. I tested the code for two (almost) simultaneous inserts to ensure that there won't be duplicates. The second client should always receive the value if it has been inserted.
And here is the line of code in my windows service that executes it:
newJob.DataScopeId = db.CreateOrRetrieveDataScope(1, areaMap.Area.ClientID, areaMap.AreaID, null, null).FirstOrDefault().Value;
The stored proc is executed for multiple rows in a table in the database. Essentially, new jobs are created from rows in another table.
So far I have tried watching the activity monitor as it executes the operation, but I couldn't glean anything exceptional there. I also tried to set the number of connections to 0 (unlimited) in SQL server management studio.
EDIT: Taking another look at the activity monitor, it appears that my stored procedure is suspended.