# sqlite3.OperationalError：数据库被锁定
  （背景：SQLite忙超时5000毫秒已超过）

- **ID:** `database/sqlite-busy-timeout-exceeded`
- **领域:** database
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

另一个连接持有写事务的时间超过忙超时时间，导致并发写入失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| SQLite 3.40 | active | — | — |
| SQLite 3.41 | active | — | — |
| SQLite 3.42 | active | — | — |

## 解决方案

1. ```
   Set a higher busy timeout: PRAGMA busy_timeout = 30000; (30 seconds) to allow longer waits for locks.
   ```
2. ```
   Switch to WAL mode: PRAGMA journal_mode=WAL; This allows concurrent reads while a write is in progress.
   ```
3. ```
   Use a connection pool with retry logic: from sqlite3 import connect; import time; for i in range(5): try: conn.execute(...); break; except sqlite3.OperationalError: time.sleep(1)
   ```

## 无效尝试

- **Set PRAGMA busy_timeout to 0 to disable timeout** — Disabling timeout causes immediate failure on lock contention, making the problem worse instead of better. (80% 失败率)
- **Use WAL mode but keep journal_mode=DELETE** — WAL mode improves concurrency for readers but does not eliminate write contention; the lock still exists. (50% 失败率)
