Retrying transactions safely
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
retry transient failures without causing duplicates.
Planned topics
- Retry only transient errors: serialization failure, deadlock, optimistic lock conflict.
- Retry the whole transaction, outside the transactional method (a new transaction per attempt).
- Limit attempts and add backoff with jitter.
- Make the unit of work idempotent (unique request key, upsert) or retries can double-charge.
- Spring Retry or Resilience4j can wrap the call.
Draft outline details
Demonstration
for (int attempt = 1; ; attempt++) {
try { return tx.execute(s -> placeOrder(cmd)); }
catch (TransientDataAccessException e) {
if (attempt == 3) throw e;
sleep(backoff(attempt));
}
}Check your understanding
Why must the retry loop be outside the transaction?
Show answer
Each attempt needs a fresh transaction; the failed one is dead.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.