Cannot use READPAST in snapshot isolation mode

Posted by Marcus on Stack Overflow See other posts from Stack Overflow or by Marcus
Published on 2010-04-14T16:21:15Z Indexed on 2010/04/14 16:23 UTC
Read the original article Hit count: 584

I have a process which is called from multiple threads which does the following:

  1. Start transaction
  2. Select unit of work from work table with by finding the next row where IsProcessed=0 with hints (UPDLOCK, HOLDLOCK, READPAST)
  3. Process the unit of work (C# and SQL stored procedures)
  4. Commit the transaction

The idea of this is that a thread dips into the pool for the "next" piece of work, and processes it, and the locks are there to ensure that a single piece of work is not processed twice. (the order doesn't matter).

All this has been working fine for months. Until today that is, when I happened to realise that despite enabling snapshot isolation and making it the default at the database level, the actual transaction creation code was manually setting an isolation level of "ReadCommitted".

I duly changed that to "Snapshot", and of course immediately received the "You can only specify the READPAST lock in the READ COMMITTED or REPEATABLE READ" error message. Oops!

The main reason for locking the row was to "mark the row" in such a way that the "mark" would be removed when the transaction that applied the mark was committed and the lock seemed to be the best way to do this, since this table isn't read otherwise except by these threads. If I were to use the IsProcessed flag as the lock, then presumably I would need to do the update first, and then select the row I just updated, but I would need to employ the NOLOCK flag to know whether any other thread had set the flag on a row.

All sounds a bit messy. The easiest option would be to abandon the snapshot isolation mode altogether, but the design of step #3 requires it.

Any bright ideas on the best way to resolve this problem?

Thanks

Marcus

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about concurrency