Repository navigation
Closed
Closed
Copy link
Labels
Milestone
Description
Environment
- JDK 21
- Spring Boot 3.4.6 + 3.5.0
- PostgreSQL 16.9 (installed with Homebrew)
- macOS 15.5
Reproducible code
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
QueryTimeoutExceptionis 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
[-]TransactionTimedOutException is not thrown when a transaction timed out on PostgreSQL[/-][+]DataAccessResourceFailureException is thrown when a transaction timed out on PostgreSQL[/+]sbrannen commentedon Jun 18, 2025
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 toSQLStateSQLExceptionTranslator.indicatesCannotAcquireLock(String).[-]DataAccessResourceFailureException is thrown when a transaction timed out on PostgreSQL[/-][+]`DataAccessResourceFailureException` thrown when transaction times out on PostgreSQL[/+]MasatoshiTada commentedon Jun 18, 2025
@sbrannen Thank you for your comment!
Can I take this issue? I'd like to send a PR.
jhoeller commentedon Jun 18, 2025
We generally expect such scenarios to throw a
SQLTimeoutExceptionthat we detect in our defaultSQLExceptionSubclassTranslator. That said, even for MySQL, we have anex.getClass().getName().contains("Timeout")check since it does not throw that exception.@MasatoshiTada could you please double-check which
SQLExceptionthe Postgres driver is actually throwing? I suppose it's not aSQLTimeoutExceptionsince otherwiseSQLExceptionSubclassTranslator(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
SQLTimeoutExceptioncheck there. From that perspective, we can add it as a general check toSQLStateSQLExceptionTranslator.Detect SQL state 57014 as QueryTimeoutException
MasatoshiTada commentedon Jun 18, 2025
@jhoeller Thank you for fixing it!
It's
org.postgresql.util.PSQLException(subclass ofjava.sql.SQLException).