I'm using Spring Boot with Ojdbc8 18.3.0.0.0 With Hikari Datasource and JPA, all query work fine. But now I need to set Query timeout for all database query I was try many way:
javax.persistence.query.timeout=1000
spring.transaction.default-timeout=1
spring.jdbc.template.query-timeout=1
spring.jpa.properties.hibernate.c3p0.timeout=1
spring.jpa.properties.javax.persistence.query.timeout=1
Config Class:
@Configuration
public class JPAQueryTimeout {
@Value("${spring.jpa.properties.javax.persistence.query.timeout}")
private int queryTimeout;
@Bean
public PlatformTransactionManager transactionManager() throws Exception {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setDefaultTimeout(queryTimeout); //Put 1 seconds timeout
return txManager;
}
}
Query:
List<Integer> llll = manager.createNativeQuery("select test_sleep(5) from dual")
.setHint("javax.persistence.query.timeout", 1).getResultList();
The database task take 5 second before return value, but in all of case, no error occor.
Could anyone tell me how to set query timeout?
@Transactional(timeout = 1)on the method that is querying the db? Be aware that the method must bepublic.PlatformTransactionManager? & use maybe proper repositories or queries with spring data jpa? & drop any other extra config you added to enforce the timeout, just use@Transactionalwithtimeouton vanilla spring data jpa