Sobes.tech
Middle

What are locks in a database?

sobes.tech AI

Answer from AI

Locks in a database are mechanisms that control concurrent access to data to prevent conflicts and ensure data integrity during parallel transaction execution.

When one transaction modifies or reads data, a lock can prevent another transaction from simultaneously modifying the same data. There are different types of locks, for example:

  • Shared lock — allows multiple transactions to read data but prohibits writing.
  • Exclusive lock — allows only one transaction to read and modify data.

Locks help implement transaction isolation levels and prevent race conditions, but can lead to deadlocks, where two transactions wait for each other to release resources.

Example: if transaction A sets an exclusive lock on a row, transaction B trying to modify the same row will wait until A completes and releases the lock.