A database deadlock can invalidate the entire transaction even when Doctrine DBAL code is inside a nested transaction or savepoint. Stop the current unit of work, discard uncertain connection state, and retry the complete operation from the outer boundary.
Last updated: October 31, 2026.
<?php
use Doctrine\DBAL\Exception\DeadlockException;
for ($attempt = 1; $attempt <= 3; $attempt++) {
try {
$connection->transactional(function ($connection): void {
updateOrder($connection);
writeAuditRecord($connection);
});
break;
} catch (DeadlockException $error) {
$connection->close();
if ($attempt === 3) {
throw $error;
}
usleep(random_int(100000, 400000));
}
}Retry the whole transactional callback, not only the last statement. Closing the DBAL connection prevents a rollback-only flag, missing savepoint, or server-aborted transaction from leaking into unrelated work.
Keep one transaction owner
Doctrine’s transaction documentation explains that nested transaction calls are emulated with a nesting counter or savepoints. A nested rollback can mark the outer transaction rollback-only. If the database has already rolled back the transaction, a later savepoint rollback may also fail because that savepoint no longer exists.
Place the transaction boundary in the application service that owns the complete operation. Lower-level methods should execute statements using the supplied connection rather than starting and committing their own independent transaction.
Retry only safe operations
Deadlock retry is appropriate only when repeating the unit is safe. Use unique constraints or idempotency keys for inserts, keep email and remote API calls outside the database transaction, and add randomized backoff. Log the database error code, attempt number, and operation identifier without recording sensitive values.
Do not mix direct PDO transaction methods with DBAL transaction methods because DBAL cannot track those state changes. Continue with handling lost MySQL connections in transactions, reading SQLSTATE details, and examining deadlock diagnostics.