Repository navigation
Description
Versions. Spring Framework 7.0.10 (spring-context, spring-jdbc), pgjdbc 42.7.14, Java 25.0.4.1; PostgreSQL 13.23 and 18.6.
What happens
A method annotated @Retryable, with its defaults, calls a @Transactional method that inserts one row. When the COMMIT's reply is lost after the server committed, the commit raises an exception, @Retryable retries, and a new transaction inserts the row again. Two rows are inserted and the caller sees a normal return.
Reproduction
@Retryable public void write(String token) calling @Transactional void insert(String token) on another bean (jdbcTemplate.update("INSERT INTO b_events (token) VALUES (?)", token)), with @EnableResilientMethods and @EnableTransactionManagement, through a TCP proxy between the client and PostgreSQL that forwards the COMMIT and then closes both sides of that one connection without forwarding the server's reply; later connections pass through untouched. The same for an autocommit insert, when the server terminates the session after committing, and when a termination ends a synchronous commit wait. A minimal reproducer of the first case is attached.
Expected
One row, or the failure reported to the caller.
Observed
Two rows and a normal return in every run (36 of n=36 over a lost reply, an injected termination and a synchronous-commit termination, each inside a transaction and in autocommit, on PostgreSQL 13.23 and 18.6). With a UNIQUE constraint on the token, the retry failed with 23505 and the caller received that error for a write that had committed (36 of n=36). Where the COMMIT never reached the server, the retry applied the row once. The archived spring-retry 2.0.13 gives the same two results (36 of n=36 without the constraint).
Why
@Retryable retries any exception by default, with three retries 1 s apart (Retryable.java:91, 128, 181 at v7.0.10). The Resilience section says of @Transactional that "the current transaction has already been rolled back by the time the retry interceptor receives the exception" (framework-docs/modules/ROOT/pages/core/resilience.adoc:152-154). That does not hold when the exception comes from a COMMIT whose reply was lost: the transaction may have committed.
A possible remedy
A note in that section that an exception raised by the commit itself can leave the outcome unknown, so a retry can apply the work twice unless the work is idempotent or the outcome is resolved first.
What this does not show
One driver; the defaults of @Retryable only, not a narrower includes such as the TransientDataAccessException of the documentation's example; how often a commit reply is lost in production.
Activity
[-]`@Retryable` around `@Transactional` runs a committed transaction again when the COMMIT reply is lost[/-][+]Doc: `@Retryable` around `@Transactional` might run a committed transaction again when the COMMIT reply is lost[/+]Add note on exception narrowing for transaction retries
Add note on exception narrowing for transaction retries