SoFunction
Updated on 2025-03-03

Causes and solutions for the MySQL lock waiting timeout problem (Lock wait timeout exceeded; try restarting transaction)

Preface

In database development and management, lock waiting timeout is a common and difficult problem. This problem is even more common for applications using MySQL, especially those using InnoDB storage engine. When multiple transactions try to access or modify the same data at the same time, lock contention may occur, which ultimately causes the transaction to time out and roll back due to the inability to acquire the lock. This article will explore in-depth the reasons, impacts and corresponding solutions for lock waiting timeout to help developers effectively deal with this problem.

What is lock waiting timeout?

Lock waiting timeout refers to when a transaction attempts to acquire a lock on a resource (such as a data row or table), if the waiting time exceeds the preset threshold (i.e., innodb_lock_wait_timeout), MySQL will return an error indicating that the transaction cannot complete. This situation is usually accompanied by a MySQLTransactionRollbackException error, and developers can often see prompts similar to "Lock wait timeout exceeded; try restarting transaction" in the log.

Type of lock

Before discussing lock waiting timeouts, it is necessary to understand the lock mechanism in MySQL. There are mainly the following types of locks in MySQL:

  1. Row-level lock: Allows multiple transactions to update different rows at the same time, suitable for high concurrency scenarios.
  2. Table-level lock: When operating on the entire table, other transactions cannot access the table.
  3. Intention lock: Used to coordinate between table-level locks and row-level locks to ensure that the intent of table-level locks can be acquired when executing row-level locks.

In the InnoDB storage engine, row-level locks are usually the default lock type, with the goal of improving concurrency performance. However, when multiple transactions compete for data on the same row, a lock waiting timeout may occur.

Common reasons why lock waiting timeout

1. Lock contention

Lock contention is the main reason why lock wait timeouts. Suppose there are two transactions A and B, both of which try to modify the same row of data:

Lock contention is the main reason why lock wait timeouts. Suppose there are two transactions A and B, both of which try to modify the same row of data:

  • Transaction A started and successfully acquired the lock for the line.
  • Transaction B tries to acquire the lock of the same line, at which point it must wait.
  • If transaction A runs for too long, transaction B will time out because it cannot acquire the lock.

2. Long-running transactions

During transaction processing, long-term transactions will hold locks and not release them, which will affect the execution of other transactions. If a transaction is not submitted or rolled back in time while performing complex calculations or performing multiple database operations, other transactions attempting to access the same resource will face the problem of lock waiting timeout.

3. Batch operation

Lock competition can intensify when large batch insertions or deletions are made. Especially the deletion operation, as it usually involves locking multiple rows or entire tables, causing other transactions to wait for the lock to be released.

Influence

Lock waiting timeout not only causes transaction failure, but also affects the performance and user experience of the application. Frequent transaction rollbacks can lead to data inconsistency, slow application responses, and even cause greater system problems such as deadlocks or resource exhaustion.

Solution

Faced with the problem of lock waiting timeout, developers can adopt a variety of strategies to mitigate or solve it. Here are some common methods:

1. Optimize transaction management

In order to reduce lock contention, developers should try to shorten the lock holding time in transactions. This can be achieved by:

  • Submit or roll back as soon as possible: After completing all necessary operations, submit the transaction immediately to avoid holding the lock for a long time.
  • Reduce transaction complexity: Split complex transactions into multiple simple transactions to ensure that each transaction operates as few as possible.

2. Adjust MySQL configuration

If lock waiting timeout occurs frequently, you can consider adjusting MySQL configuration:

  • Enlargementinnodb_lock_wait_timeout: This is the setting of the lock waiting timeout in MySQL, and the default value is usually 50 seconds. Increasing this value can give the transaction more waiting time, but it cannot fundamentally solve the lock contention problem.
  • Adjust transaction isolation level: Convert the isolation level of the transaction fromREPEATABLE-READLower toREAD-COMMITTED, can reduce the lock holding time.

3. Implement the retry mechanism

Capture in codeLock wait timeout exceededAfter an error, you can set a retry mechanism. When such an exception occurs, the transaction is retryed. This method is especially suitable for operations that require multiple attempts.

public void executeWithRetry(Runnable task) {
    int attempts = 0;
    while (attempts < MAX_RETRIES) {
        try {
            ();
            return; // Execute successfully, exit        } catch (CannotAcquireLockException e) {
            attempts++;
            if (attempts >= MAX_RETRIES) {
                throw e; // Exceed the maximum number of retry times, throw an exception            }
            // Wait for a while before trying again            try {
                (RETRY_DELAY);
            } catch (InterruptedException interruptedException) {
                ().interrupt(); // Restore interrupted state            }
        }
    }
}

4. SQL query optimization

To optimize SQL queries to reduce lock contention, consider the following strategies:

  • Avoid full table locking: In executionDELETEDuring operation, ensure that the conditions can effectively lock the target data. Use indexes to speed up finding and reduce the number of locked rows.
  • Use index reasonably: Ensure that all queries make full use of indexes and avoid unnecessary full table scanning.

5. Batch processing

For large-scale deletion or insertion operations, batch processing can effectively reduce the lock contention of a single operation. For example, in the deletion operation, the number of deleted rows can be limited to a reasonable range:

public void deleteDataInBatches(int batchSize) {
    int deletedRows;
    do {
        deletedRows = executeDelete(batchSize);
    } while (deletedRows > 0);
}

private int executeDelete(int batchSize) {
    return ("DELETE FROM statistics_data WHERE condition LIMIT ?", batchSize);
}

6. Check deadlock status

If there is a deadlock, MySQL will automatically roll back one of the transactions. useSHOW ENGINE INNODB STATUSThe command can view deadlock information and further optimize the table structure and query to reduce the probability of deadlock.

in conclusion

The lock waiting timeout problem is very common in high concurrency database applications. Understanding its root causes, impacts and optimization strategies will help developers manage database transactions more effectively and improve system stability and performance. By optimizing transaction management, adjusting database configuration, implementing retry mechanisms and SQL query optimization, the probability of lock waiting timeout can be greatly reduced, thereby building more efficient and reliable applications. For any developer involved in database operations, it is very important to master these knowledge and skills.

The above is the detailed content of the MySQL lock waiting timeout problem and the solution (Lock wait timeout exceeded; try restarting transaction). For more information about MySQL lock waiting timeout, please pay attention to my other related articles!