# java.sql.SQLException: 锁超时。锁请求超时，因为另一个事务持有冲突的锁。

- **ID:** `java/derby-lock-timeout`
- **领域:** java
- **类别:** resource_error
- **错误码:** `DERBY-1000`
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

数据库事务正在等待另一个事务持有的锁，并超过了配置的锁超时时间，通常是由于嵌入式 Derby 数据库中的长时间运行事务或死锁。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Apache Derby 10.14 | active | — | — |
| Apache Derby 10.15 | active | — | — |
| Apache Derby 10.16 | active | — | — |

## 解决方案

1. ```
   Set a shorter lock timeout using Derby system property derby.locks.waitTimeout=30 (in seconds) to fail fast and retry the transaction.
   ```
2. ```
   Optimize transaction scope: commit or rollback transactions as early as possible to release locks. For example, in JDBC code, use try-with-resources to ensure auto-close and commit.
   ```
3. ```
   Use isolation level READ_COMMITTED instead of REPEATABLE_READ to reduce lock contention. Set connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED).
   ```

## 无效尝试

- **Restart the database server or application** — Restarting clears all locks but does not fix the underlying transaction logic that causes contention; the problem will recur. (90% 失败率)
- **Increase the lock timeout to a very high value (e.g., 600 seconds)** — A higher timeout delays the error but does not resolve the conflict; the transaction may still time out or cause performance degradation. (70% 失败率)
- **Use a different database driver version** — The lock timeout is a database-level behavior, not a driver issue; changing drivers does not affect locking mechanisms. (85% 失败率)
