I am trying to do the following in a highly concurrent system written in C#.
- Aquire row-lock on a data-item in transaction scope
- Do a lot of db-interaction
- if anyhing goes wrong
- Update locked data-item, that processing failed
- Commit update
- Roll back any changes in step 2 and release lock on data item
However - I have no idea on how to achieve this, without releasing the item lock before doing the update. My current (bad ) solution is this:
- Aquire row-lock on data-item in transaction scope
- Do a lot of db-interaction
- if anyhing goes wrong
- Roll back any changes in step 2 and release lock on data item
- Reaquire lock on data-item in new transaction scope
- Update locked data-item, that processing failed
- Commit update
The problem with this solution, is that I release the lock, so that another process can pick up the item before I have had a change to write to the item that processing has failed.
Is there any way to create a nested transaction scope, that can commit on an item locked by an outer transaction scope, while still allowing the outer scope to roll back?
It appears that all my trouble comes from the fact, that the outer scope has a lock on the item that the inner scope want to update.