Skip to content

DataAccessResourceFailureException thrown when transaction times out on PostgreSQL #35073

Closed

Description

@MasatoshiTada

Environment

  • JDK 21
  • Spring Boot 3.4.6 + 3.5.0
  • PostgreSQL 16.9 (installed with Homebrew)
  • macOS 15.5

Reproducible code

https://github.com/MasatoshiTada/transaction-timeout-sample-jdbc/blob/main/src/test/java/com/example/transactiontimeoutsamplejdbc/SampleServiceTest.java

What happens

My repository class:

@Repository
public class SampleRepository {
  private final JdbcTemplate jdbcTemplate;

  public SampleRepository(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }

  public List<Sample> selectAll() {
    return jdbcTemplate.query("SELECT id, name FROM sample ORDER BY id", new DataClassRowMapper<>(Sample.class));
  }

  public Object sleep(int seconds) {
    return jdbcTemplate.queryForObject("SELECT pg_sleep(?)", new Object[]{seconds}, Object.class);
  }

  public void insert(Sample sample) {
    jdbcTemplate.update("INSERT INTO sample(id, name) VALUES (?, ?)", sample.id(), sample.name());
  }
}

My service class. registerWithSleep() will timed out in 2 seconds.

@Service
public class SampleService {
    private static final Logger logger = LoggerFactory.getLogger(SampleService.class);

    private final SampleRepository sampleRepository;

    public SampleService(SampleRepository sampleRepository) {
        this.sampleRepository = sampleRepository;
    }

    @Transactional(timeout = 2, readOnly = false)
    public void registerWithSleep(Sample sample, int seconds) {
        logger.info("Sleep {}seconds...", seconds);
        sampleRepository.sleep(seconds);
        logger.info("Sleep completed. Starting INSERT...");
        sampleRepository.insert(sample) ;
        logger.info("INSERT completed.");
    }
}

This method times out in 2 seconds as I expected, but throws DataAccessResourceFailureException.

Expected behavior

TransactionTimedOutException is thrown?

I could see QueryTimeoutException is thrown on MySQL 8.0.

Why does it behave like this?

When the transaction times out, PostgreSQL returns the SQL state "57014" (query_cancelled).

PostgreSQL JDBC Driver cancells a query with TimerTask (see here)

Since sql-error-codes.xml does not contain "57014", Spring fallbacks to SQLStateSQLExceptionTranslator that has "57" (DB2: out-of-memory exception / database not started) in DATA_ACCESS_RESOURCE_FAILURE_CODES.

Activity

  1. added
    status: waiting-for-triage
    on Jun 18, 2025
  2. changed the title [-]TransactionTimedOutException is not thrown when a transaction timed out on PostgreSQL[/-] [+]DataAccessResourceFailureException is thrown when a transaction timed out on PostgreSQL[/+] on Jun 18, 2025
  3. added
    in: data
    on Jun 18, 2025
  4. sbrannen commented on Jun 18, 2025

    @sbrannen
    Member

    Hi @MasatoshiTada,

    Congratulations on reporting your first issue for the Spring Framework! 👍

    It appears that we could handle this particular scenario by adding custom logic for PostgreSQL in SQLStateSQLExceptionTranslator.doTranslate(String, String, SQLException), somewhat analogous to SQLStateSQLExceptionTranslator.indicatesCannotAcquireLock(String).

  5. changed the title [-]DataAccessResourceFailureException is thrown when a transaction timed out on PostgreSQL[/-] [+]`DataAccessResourceFailureException` thrown when transaction times out on PostgreSQL[/+] on Jun 18, 2025
  6. MasatoshiTada commented on Jun 18, 2025

    @MasatoshiTada
    Author

    @sbrannen Thank you for your comment!
    Can I take this issue? I'd like to send a PR.

  7. jhoeller commented on Jun 18, 2025

    @jhoeller
    Contributor

    We generally expect such scenarios to throw a SQLTimeoutException that we detect in our default SQLExceptionSubclassTranslator. That said, even for MySQL, we have an ex.getClass().getName().contains("Timeout") check since it does not throw that exception.

    @MasatoshiTada could you please double-check which SQLException the Postgres driver is actually throwing? I suppose it's not a SQLTimeoutException since otherwise SQLExceptionSubclassTranslator (which is in the fallback chain) would have detected it?

    On a positive note, 57014 seems to be universal enough in terms of semantics: It also means query timeout on DB2 (originally an "operator-cancelled query" where someone would manually cancel it I guess), it's just usually covered by the SQLTimeoutException check there. From that perspective, we can add it as a general check to SQLStateSQLExceptionTranslator.

  8. added
    type: enhancement
    and removed
    status: waiting-for-triage
    on Jun 18, 2025
  9. added this to the 6.2.9 milestone on Jun 18, 2025
  10. self-assigned this
    on Jun 18, 2025
  11. added a commit that references this issue on Jun 18, 2025
    e33adad
  12. MasatoshiTada commented on Jun 18, 2025

    @MasatoshiTada
    Author

    @jhoeller Thank you for fixing it!

    could you please double-check which SQLException the Postgres driver is actually throwing? I suppose it's not a SQLTimeoutException since otherwise SQLExceptionSubclassTranslator (which is in the fallback chain) would have detected it?

    It's org.postgresql.util.PSQLException (subclass of java.sql.SQLException ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: datatype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions