Question 1
A financial services application is experiencing deadlocks during high-concurrency periods. The problematic transaction involves updating a user's balance and inserting a record into a transaction log table. The database uses the default REPEATABLE READ isolation level. Analysis reveals that two concurrent transactions often attempt to lock the same range of rows in the transaction log table, which is indexed by transaction_date. Which strategy is most effective at resolving these deadlocks while maintaining data consistency?
Answer and explanation
Correct answer: A
Changing the isolation level to READ COMMITTED for these specific transactions is the best solution. REPEATABLE READ uses gap locks, which can lock the space between index records, leading to a higher chance of deadlocks when inserting into a sequentially indexed column. READ COMMITTED does not use gap locks for ordinary statements, which significantly reduces the likelihood of this type of deadlock. Switching to SERIALIZABLE would worsen the problem by increasing locking. Using LOCK TABLES is too coarse and would serialize access, killing concurrency. Retrying transactions is a valid strategy but doesn't solve the root cause of the frequent deadlocks.