Metadata merge operations fail with 'Lock wait timeout exceeded' on large batches — long-running transaction anti-pattern #1473
Replies: 2 comments
🔗 Related PRs#234 - fix(api): skip physical books in metadata manager to prevent NPE [merged] 📝 Issue PlannerCheck the box below or use the
🧪 Issue enrichment is currently in open beta.You can configure auto-planning by selecting labels in the issue_enrichment configuration. To disable automatic issue enrichment, add the following to your issue_enrichment:
auto_enrich:
enabled: false💬 Have feedback or questions? Drop into our discord! |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Description
When merging metadata values (authors, categories, languages, etc.) that affect a large number of books, the operation fails with a database lock timeout. This happens because the entire merge is processed inside a single
@Transactionalmethod that holds row locks onbook_metadataandbook_filefor the entire duration.Steps to Reproduce
Expected Behavior
The merge should succeed regardless of batch size. Large merges should be processed in smaller sub-transactions to avoid holding database locks for extended periods.
Actual Behavior
Root Cause
In
MetadataManagementService.consolidateLanguages()(and similar methods for authors, categories, etc.):The
@TransactionalonconsolidateMetadata()means all 497 books are processed in one giant transaction. File I/O (writing metadata to PDF/CBZ files) is slow, so the transaction holds InnoDB row locks for minutes, exceedinginnodb_lock_wait_timeout(default 50s).Environment
innodb_lock_wait_timeout = 50)Suggested Fix
Option 1 — Batch in smaller chunks with intermediate commits:
Option 2 — Use
Propagation.REQUIRES_NEWinwriteMetadataToFile()so each book commits independently:Option 3 — Decouple file writing from DB transaction:
Related Issues
Both issues stem from the same architectural problem: metadata operations do too much work (file I/O + DB updates) inside a single transaction.
All reactions