Labs ICT
โญ Pro Login

Concurrency Control

Locks, deadlocks, and handling simultaneous access.

Handling Simultaneous Access

When multiple transactions run at the same time, they can interfere with each other. Concurrency control mechanisms ensure that concurrent transactions produce correct results without sacrificing performance.

Isolation Levels


  Level                โ”‚ Dirty Read โ”‚ Non-Repeatable โ”‚ Phantom
  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  Read Uncommitted     โ”‚ Yes        โ”‚ Yes            โ”‚ Yes
  Read Committed       โ”‚ No         โ”‚ Yes            โ”‚ Yes
  Repeatable Read      โ”‚ No         โ”‚ No             โ”‚ Yes
  Serializable         โ”‚ No         โ”‚ No             โ”‚ No

Higher isolation = more safety but less concurrency. Choose based on your needs.

Locks


  Shared Lock (S):    Multiple transactions can READ simultaneously
  Exclusive Lock (X): Only ONE transaction can WRITE at a time

  Transaction A reads row:     Shared lock on row
  Transaction B reads row:     Shared lock on row (OK โ€” both read)
  Transaction C writes row:    Exclusive lock โ€” must wait for A & B

Deadlock


  Transaction A:                 Transaction B:
  Locks Row 1                    Locks Row 2
  Wants Row 2 (waits for B)     Wants Row 1 (waits for A)
  ...waiting forever...          ...waiting forever...

  Solution: Database detects deadlock and rolls back one transaction

๐Ÿงช Quick Quiz

What is a deadlock in a database?