Skip to content

Instantly share code, notes, and snippets.

@timsmelik
Created January 15, 2024 15:48
Show Gist options
  • Save timsmelik/0691f617eb515593264cc6a412c0ca2d to your computer and use it in GitHub Desktop.
Save timsmelik/0691f617eb515593264cc6a412c0ca2d to your computer and use it in GitHub Desktop.
Spring Boot - Postgres read timeout

Spring Boot + Postgres Read Timeout

The read timeout on a Postgres connection cannot be set with standard properties. We have to use a custom property under the following key.

spring.datasource.hikari.data-source-properties

A list of properties that can be used for the Postgres driver can be found on this page.

So, in order to set the socket timeout, we have to set the following key.

spring.datasource.hikari.data-source-properties.socketTimeout=20

Testing the timeout

To test the timeout, we need to be able to control how long it takes to perform a query. I found having an explicit sleep in the query sent to Postgres was the easiest solution. You can do this by adding pg_sleep to your query. For example, if you want to delay fetching a record with a query like this:

SELECT * FROM some_table

You could use pg_sleep in the following manner:

SELECT *, pg_sleep(10) FROM some_table

The query will now be delayed by (at least) 10 seconds and return an extra column in addition to the columns in the table.

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