As the title says, do I have read/write access to my entire database while it is rolling back a transaction?
Or read/write access to only the tables that the transaction did not write to? What is the locking behavior?
If I do have read access, what happens when I am reading a table where a transaction rolling back wrote to (does the query fail or just wait)?
Sorry for the questions, I have tried reading online (ROLLBACK TRANSACTION (Transact-SQL)), but it seems to not touch on this level of depth.
Best Answer
The answer really depends largely on your isolation level, what type of locks are held by the transaction being rolled back, and what your non-rollback session is trying to do.
Step 1) What is locked?
Before the
ROLLBACK
was issued, that transaction had done work and acquired locks. You can see details on those locks by looking insys.dm_tran_locks
:The
resource_type
andresource_associated_entity_id
will tell you what is locked. If theresource_type
is "Object" thenresource_associated_entity_id
is anobject_id
. Ifresource_type
is "Key" or "Page" thenresource_associated_entity_id
is ahobt_id
. There are more types to decode, but those two cover most scenarios.Lastly, the lock mode gives information on how that lock is being used. BOL gives a rundown of all the different lock modes.
Step 2: What will be blocked?
If the transaction being rolled back does not have a lock on an object, then the
ROLLBACK
won't block other sessions. That part is easy.Whether these locks cause blocking is going to depend on your isolation level, and what you're trying to do. There's a very in-depth series on isolation levels by Paul White, which you could read for lots of details.
Different combinations of reading, writing, and isolation levels will have different blocking outcomes. Generally speaking, writes always block writes. However, writes may or may not block reads. Under SQL Server's default
READ_COMMITTED
isolation level, writes will block reads. If you are usingREAD_COMMITTED_SNAPSHOT
(aka "RCSI" aka "Read Committed with Row Versioning"), then writes will not block reads.In all cases, if another session is blocked by the session in
ROLLBACK
, the waiting session would just wait until theROLLBACK
released the lock.