sql - What type of Transaction IsolationLevel should be used to ignore inserts but lock the selected row? -


I have a process that starts a transaction, inserts a record in Table 1, and then a Long moving web service (up to 30 seconds) If the web service call fails then the merger is brought back (which we want). Here is an example of the inclusion (this is actually several entries in multiple tables, but I am making it simple for this question):

  INSERT in table 1 (User ID, Statistic) values (@UserId, 1)  

I have another process that asks Table 1 in the first step:

  SELECT TOP 1 * to Table 1 Where position type = 2  

and then updates that line for that user. When the process 1 is running, the table 1 gets locked, the process 2 will not complete until the process 1 ends, which is a problem because a long delay is introduced, while the process 1 will be your web service Ends the call.

Process 1 Enter a StatusTypeId of only 1 and it is only an operation that is included in Table1 process 2 will inquire only on StatusTypeId = 2. I want to tell Process 2 to ignore any insertion in Table 2, but blocks the line that he chooses. Default isolation level for Process 2 is waiting too much, but I'm afraid algaevalaval. Read permission allows reading too much dirty data. I do not want two users to run Process 2 and then accidentally get the same line

Is there a separate isolation to use other than Reddit, which says that focus on the lines inserted Do not give, but make sure to select the selected row select locks?

This topic should be blocked from being included in the selection by providing the appropriate indexes.

Test table.

  Insert table 1 (UNID INIT primary key, StatStipId INT, one more column varchar (50)) from Table 1 to selected number (LN (type)% 2) 1, NIID ( ) From MasterDisco. Spt_values ​​where type = 'P'  

query window one

  BEGIN TRAN Table 1 (User ID, Statistic ID) values Include (5000, 1) WAITFOR DELAY '00: 01 '; Rollback  

query window two (block)

  SELECT TOP 1 * from table 1 where position type = 2 command other column  

But if you try again after adding an index, then it will not block Table 1 (StatCat ID, AOC column) on nonclassed index ix

Regarding locking rows for your code> Process 2 You can use the following ( READPAST Signal 2 will allow concurrent process 2 to begin the process of different rows instead of blocking each other) You can search relevant by Remus Rusanu

  Back Tran with Top 1 * (UPDLOCK, READPAST) where the position = 2 order other column / * rest of the code two code * / COMMIT  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -