Noah Martin
Noah Martin

Reputation: 1809

spring.jpa.properties.javax.persistence.query.timeout not working for PostgreSQL

I would like to have an option to limit all Spring jpa queries to a certain time limit. If a query takes longer than the specified limit it should be cancelled. From what I saw this can be achieved by using javax.persistence.query.timeout and since I am in a Spring context:

spring: 
  jpa:
    properties:
      javax.persistence.query.timeout: 10000

This should limit all queries executed from spring repositories to 30 seconds, but it's not working. This repository method takes 20 seconds:

@Query(value = "select count(*) from pg_sleep(20)", nativeQuery = true)
int slowQuery();

I tried it with Postgresql 9.6.11 and with Postgresql 10.5.

Is there some problem with these Postgres versions? Is there any other way to achieve the query execution timeout?

Upvotes: 6

Views: 12256

Answers (2)

tariksahin4391
tariksahin4391

Reputation: 1

You should also add the suitable property depends on your database.

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

Upvotes: 0

Mauricio Hartmann
Mauricio Hartmann

Reputation: 109

The following code should be a good alternative.

YAML file:

spring:
    jpa:
        properties:
            javax:
                persistence:
                    query:
                        timeout: 10000

Upvotes: 6

Related Questions