# RuntimeError: 在 fixture 'database' 的 teardown 过程中出错 (得到异常: OperationalError: database is locked)

- **ID:** `python/pytest-fixture-finalizer-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

fixture 的 finalizer（teardown）抛出异常，通常是因为在尝试关闭数据库连接时，连接仍在使用中或被锁定。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Ensure all database transactions are committed or rolled back before teardown: `yield db; db.close()`
   ```
2. **** (95% 成功率)
   ```
   Use a context manager for the database connection to guarantee proper cleanup.
   ```

## 无效尝试

- **** — This is unreliable; the lock might persist longer, and sleeping wastes time. (70% 失败率)
- **** — This hides potential resource leaks and can cause issues in subsequent tests. (85% 失败率)
