java.sql.SQLException: 锁超时。锁请求超时,因为另一个事务持有冲突的锁。
java.sql.SQLException: Lock time out. The lock request timed out because another transaction holds a conflicting lock.
ID: java/derby-lock-timeout
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Apache Derby 10.14 | active | — | — | — |
| Apache Derby 10.15 | active | — | — | — |
| Apache Derby 10.16 | active | — | — | — |
根因分析
数据库事务正在等待另一个事务持有的锁,并超过了配置的锁超时时间,通常是由于嵌入式 Derby 数据库中的长时间运行事务或死锁。
English
A database transaction is waiting for a lock held by another transaction and exceeds the configured lock timeout, typically due to long-running transactions or deadlocks in embedded Derby databases.
官方文档
https://db.apache.org/derby/docs/10.15/ref/rrefexceptlocktimeout.html解决方案
-
Set a shorter lock timeout using Derby system property derby.locks.waitTimeout=30 (in seconds) to fail fast and retry the transaction.
-
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.
-
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
90% 失败
Restarting clears all locks but does not fix the underlying transaction logic that causes contention; the problem will recur.
-
Increase the lock timeout to a very high value (e.g., 600 seconds)
70% 失败
A higher timeout delays the error but does not resolve the conflict; the transaction may still time out or cause performance degradation.
-
Use a different database driver version
85% 失败
The lock timeout is a database-level behavior, not a driver issue; changing drivers does not affect locking mechanisms.