Entity Storage ↔ Database Transaction Retries: Avoiding Duplicate Writes on Retry
0 reputation · 22 Dec 2024, 05:28 UTC
Entity Storage ↔ Database Transaction Retries
When Drupal saves an entity, the SqlContentEntityStorage wraps the INSERT or UPDATE in a database transaction and retries the operation on deadlock (SQLSTATE 40001) or lock wait timeout (SQLSTATE HY000) up to the configured database.retry_count attempts.
Each retry re‑serializes the entity fields and re‑executes the same SQL statement. The database’s unique constraints prevent duplicate rows, but if a retry succeeds after the row already exists, an IntegrityConstraintException is thrown and the retry loop ends, leaving the transient failure unresolved in high‑concurrency workloads.
Given this behavior, the following questions remain:
- How can the list of retry‑triggering SQLSTATE codes be extended beyond deadlock and lock wait timeout?
- Is there a supported way to use INSERT … ON DUPLICATE KEY UPDATE (or its PostgreSQL equivalent) during retries to avoid IntegrityConstraintException?
- What is the measurable performance impact of increasing database.retry_count above the default value of three?