Understanding transaction locking mechanisms and concurrency control
SQL Server uses locks to maintain data consistency and isolation between transactions. Without locks, multiple users could update the same row simultaneously, causing issues like dirty reads, lost updates, or inconsistent data.
Lock Type | Purpose | Example |
---|---|---|
Shared (S) | Read-only access. Multiple transactions can read simultaneously. | SELECT query |
Exclusive (X) | Write access. Only one transaction can modify data at a time. | UPDATE, DELETE, INSERT |
Update (U) | Prevents deadlocks when a transaction plans to update a row. | Often used with SELECT ... FOR UPDATE |
Intent Locks (IS, IX) | Show intention to acquire a finer-grained lock (row/page). | SQL Server manages hierarchical locks efficiently |
Schema locks | Protect schema changes like adding or altering columns. | ALTER TABLE |
Bulk Update (BU) | Used during bulk copy operations. | BULK INSERT |
SQL Server tries to use the smallest possible lock, but may escalate to table-level if too many locks are held (lock escalation).
WITH (NOLOCK)
or snapshot isolation.A deadlock occurs when two transactions wait on each otherβs locks indefinitely:
Transaction A: locks Row 1 β wants Row 2 Transaction B: locks Row 2 β wants Row 1
SQL Server automatically detects this and kills one transaction to resolve the deadlock.
WITH (NOLOCK)
β read without locking (may read uncommitted data)WITH (UPDLOCK)
β acquire update lockWITH (XLOCK)
β force exclusive lockRow A --------- | Data | --------- [Transaction 1: S] Row A [Transaction 2: S]
Both can read simultaneously; no writes allowed.
[Transaction 1: S] Row A [Transaction 2: S] --> Transaction 3 waits [Transaction 3: X] Row A
Exclusive lock prevents other reads or writes until the transaction completes.
Transaction 4: SELECT * FROM Table WHERE ID=1 WITH (UPDLOCK) [U] Row is locked to prevent exclusive writes by others. Converts to exclusive lock when UPDATE happens.
Row A Row B ------ ------ Transaction 5: X --> Row A, wants Row B Transaction 6: X --> Row B, wants Row A
Both wait indefinitely. SQL Server resolves by killing one transaction.