tags:

views:

39

answers:

1

I have a couple questions concerning Update (U) locks and Exclusive (X) locks.

1) Am I correct that an 'X' lock is put on a resource when the resource is about to get updated?

2) I'm a little fuzzy on U locks. Am I correct that a U lock is applied when a resource is read and SQL Server thinks it might need to update the resource later? If this is correct, would a 'U' lock only get applied when a read is being done within the context of a transaction? I guess I'm trying to understand under what circumstances SQL Server thinks it might need to update later a row it just read now.

Thanks - Randy

+1  A: 

1) Am I correct that an 'X' lock is put on a resource when the resource is about to get updated?

Yes.

2) I'm a little fuzzy on U locks. Am I correct that a U lock is applied when a resource is read and SQL Server thinks it might need to update the resource later? If this is correct, would a 'U' lock only get applied when a read is being done within the context of a transaction? I guess I'm trying to understand under what circumstances SQL Server thinks it might need to update later a row it just read now.

U locks are compatible with the read locks but not with each other, X locks are not compatible even with the read locks.

U locks are placed by DML queries (UPDATE, DELETE, MERGE) while scanning the table rows (no decision to update is made yet), while X locks are placed when the decision is made to update the row.

In READ COMMITTED isolation mode, update locks are lifted after the record was evaluated to be left as is, in higher isolation modes they are kept until the end of the transaction.

Quassnoi
@Quassnoi - I'm still a little confused on 'U' locks. How does SQL Server determine that a row that was just read might be updated in the future? Is it because the read was done within the context of a transaction? I'm trying to determine why a U lock is used sometimes instead of an 'S' lock.
Randy Minder
@Randy: `U` locks are used for `DDL` queries (`UPDATE`, `DELETE`, `MERGE`, `SELECT FOR UPDATE`). `S` locks are used for read-only queries (`SELECT`)
Quassnoi
@Randy: `REPLACE('DDL', 'DML')` in the comment above
Quassnoi
Actually U locks and S locks are a bit more complicated, they are asymmetric: U is compatible with S, but S is incompatible with U. This way the U lock may be granted even if there are S locks held on the resource. Further S locks requests will block, because they're incompatible with the U. By the time the writer needs to actually make the write, he can upgrade the U to X without the danger of being starved by S locks sneaking in.
Remus Rusanu
@Remus - Great explanation. Thank you.
Randy Minder
@Remus: do you want to say that `BEGIN TRANSACTION SELECT * FROM mytable WITH (UPDLOCK, HOLDLOCK)` will block the concurrent `SELECT` queries?
Quassnoi
http://amzn.to/bSwmW9 p. 410: "The update lock mode converts deadlocks into lock waits. It is chosen to be asymmetric: update is compatible with share but share is not compatible with update. This allows the updater to read but delays other transaction readers and updaters, since the the transaction is about to update the record". At least, that's how the theory goes. In SQL Server they actually behave like you explained.
Remus Rusanu