0

How to handle Concurrency issue?

I am sending two files.1 file from frontend UI and 1 file from backend.

Suppose File A content is

Hello
I am XYZ

Suppose File B content is

Hello
I am ABC

When i am sending the files at the same time then the both files are getting merged into one like below:

Hello
I am XYZ
Hello
I am ABC

Is there any Solution to handle this Concurrency issue? I have used transaction.

I have the following Script:

BEGIN TRY
BEGIN TRANSACTION 
  SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

  EXEC PROC_FILE_IDENTIFIER @FILE_IDENTIFIER OUT
  
   IF @IS_FL_GN=1
   BEGIN
          EXEC PROC_FLOW_GEN_CREATE_FILE @REGID,@DATAFLOW,....... ------ File Creation from UI
    END
    else
    BEGIN
          EXEC PROC_CREATE_FILE @REGID,@DATAFLOW,.........  ------File Creation from backend
    END
    
    -- Move the created file into IN folder
    
    SET @RESULTTEXT=@T_HEADER + @T_GROUP + @T_FOOTER  
       
  DECLARE @PATH       VARCHAR(2000)  
  DECLARE @TXT        VARCHAR(2000)   
  SET @PATH = 'D:\Symbio_Data_Testing\IN\' + @FILE_IDENTIFIER + '.USR'
  
   COMMIT TRANSACTION 
   END TRY
   
   BEGIN CATCH
      Select ERROR_MESSAGE()
       Select ERROR_LINE()
        IF @@TRANCOUNT > 0
          ROLLBACK TRANSACTION
            Exec PROC_CHECK_LAST_FILE_GENERATED 
   END CATCH

The SP PROC_FILE_IDENTIFIER gives the Filename. This SP contains table: File_number_mst_t which contains file_number which gets updated everytime.

Suppose there is file number 1.
   when user create file,it will take the filename as 1(f1) and update the filenumber to 2.
   Next time when the user will send file,it will take the filename as 2(f2) and update the file to 3.




 Transaction1              
   1
   2              Transaction2   
                      2
                      3
                  

How can i handle the Concurrency isssue when file Comes at the same time.(Conflict occured). Any Suggestions or ideas will be highly appreciated.

chetan kambli
  • 794
  • 5
  • 21
  • I can't follow the details of your question, but if you are having some weird concurrency issue with what seems like something to do with dirty reads, why do you set the transaction isolation level to `read uncommitted`? That allows dirty reads. – allmhuran Jul 27 '20 at 14:01
  • can you tell me what should i set the isolation level?@allmhuran – chetan kambli Jul 27 '20 at 14:04
  • 1
    The default isolation level is read committed. If you don't explicitly set any level, that is what the execution will use, unless it was set to something else in calling code. If you want to be sure, you can set it explicitly: `set transaction isolation level read committed` – allmhuran Jul 27 '20 at 14:06
  • @allmhuran i set the isolation level to commited but still facing the same issue.The files are getting merged. – chetan kambli Jul 28 '20 at 10:18
  • Sorry, given the information in the question that was the only lead I had for you. I can't really decipher anything more detailed. – allmhuran Jul 28 '20 at 10:26
  • @allmhuran i got this issue after making isolation level to commited. Transaction (Process ID 55) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. – chetan kambli Jul 29 '20 at 07:05
  • We can't see what's happening inside your two procedures, so it's hard to give any more advice. In general, if two processes take locks on the same objects, but in different orders, that can cause deadlocks. For example, if procedure1 does an insert on table T, then an update on table U, and procedure 2 does an update on table U, and then an insert on table T. – allmhuran Jul 29 '20 at 07:11

0 Answers0