简体   繁体   中英

SELECT statement won't return row first time after INSERT commit transaction

I have two stored procedures, one AddReportsApi for inserting data with a BEGIN TRANSACTION and COMMIT TRANSACTION , and the other GetReportsApi for selecting the data for inserted row.

I call the INSERT stored procedure first, then I call the SELECT stored procedure but sometimes it does not return any rows for the passed in SearchItemId which is used in the WHERE predicate.

If I execute the SELECT stored procedure a second time, it returns the expected rows.

Is there a delay in inserting the data to the table? Please note that the stored procedures are called from HangFire background job framework. From my test, HangFire should not affect the INSERT and SELECT stored procedure calls. The INSERT stored procedure is called multiple times within a minute to insert the records into the ReprotsApi table.

Insert stored procedure:

CREATE PROCEDURE [dbo].[AddReportsApi]       
   @OrderID nvarchar(50),
   @SearchItemId nvarchar(50),
   @SubjectID nvarchar(50),
   @SearchType nvarchar(50),
   @ApiName nvarchar(50),
   @ApiRequest text,
   @ApiResponse text,
   @IsActive bit,
   @CreatedOn datetime,
   @CreatedBy nvarchar(50),
   @ModifyOn datetime,
   @ModifyBy nvarchar(50),
   @HitType nvarchar(2)    
AS    
BEGIN    
   SET NOCOUNT ON;    

   BEGIN TRANSACTION  
       INSERT INTO [dbo].[ReportsApi] ([OrderID], [SearchItemId], [SubjectID], [SearchType],    
                                       [ApiName], [ApiRequest], [ApiResponse], [IsActive],
                                       [CreatedOn], [CreatedBy],
                                       [ModifyOn], [ModifyBy], [HitType])    
       VALUES (@OrderID, @SearchItemId, @SubjectID, @SearchType,
               @ApiName, @ApiRequest, @ApiResponse, @IsActive,
               @CreatedOn, @CreatedBy,
               @ModifyOn, @ModifyBy, @HitType)    

       IF (@@ERROR != 0)    
       BEGIN    
           ROLLBACK TRANSACTION    
       END    
       ELSE    
           COMMIT TRANSACTION  
END 

SELECT stored procedure:

CREATE PROCEDURE [dbo].[GetReportsApi]         
   @OrderID nvarchar(50)      
  ,@SearchItemId nvarchar(50)      
  ,@SubjectID nvarchar(50)    
  ,@CreatedBy nvarchar(50)      
AS      
BEGIN           
 SET NOCOUNT ON;      



 SELECT [Id]      
      ,[OrderID]      
      ,[SearchItemId]      
      ,[SubjectID]      
      ,[SearchType]      
      ,[ApiName]      
      ,[ApiRequest]      
      ,[ApiResponse]      
      ,[IsActive]      
      ,[CreatedOn]      
      ,[CreatedBy]      
      ,[ModifyOn]      
      ,[ModifyBy]      
      ,[HitType]      
  FROM [dbo].[ReportsApi] WHERE  [SearchItemId] = @SearchItemId
END 

it might be because indexes are being rebuilt under the hood after the insert completes. This can give dirty/phantom reads. If you have an index on [SearchItemId] then the 2nd query might use this but the index may still being refreshed.

This can even affect clustered indexes if you are inserting into the middle of the B-Tree.

It might be worth sticking a sleep(10000) or WAITFOR DELAY '00:00:10'; into your code... (That's 10 sec but you could experiment with different timings.)

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM