Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure custom database connection timeout in Spring Boot application?

In my Spring boot(2.0.7 RELEASE) application I am not able to manually set/override the timeout for the database connections in the application.properites file. I am using JPA, Hibernate, Tomcat connection pool and Postgres.

I've researched thoroughly and found very similar questions :

  • Overriding timeout for database connection in properties file
  • JPA query timeout parameters ignored but @Transaction annotation works

The reason I ask new question is because neither of the questions above have an accepted answer nor a confirmed working solution. I tried including each proposed solution in my application.properties file with no success.

Also, as mentioned in question 2: if I add parameter 'timeout = someSeconds' in the @Transactional annotation, the connection timeouts as expected but if I try extracting it in the application.properties it fails and timeouts for the default time. The problem here is that I want all connections to timeout in the given time not only the transactions.

Things I've tried in the application.properties (The desired timeout is 4 seconds):

  • spring.jpa.properties.javax.persistence.query.timeout=4000
  • spring.jdbc.template.query-timeout=4
  • spring.transaction.defaultTimeout=4
  • spring.datasource.tomcat.validation-query-timeout=4

Materials I've read:

  • http://www.masterspringboot.com/configuration/web-server/configuring-tomcat-connection-pool-on-spring-boot
  • https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
  • https://www.baeldung.com/spring-boot-tomcat-connection-pool
  • https://www.objectdb.com/java/jpa/query/setting#Query_Hints_

Am I missing some property? Does anyone know why the timeout can't be overridden via the application.properties file?

Thanks in advance.

like image 316
yordanoff Avatar asked Apr 04 '19 08:04

yordanoff


People also ask

How do I set timeout in spring boot?

@Transactional Timeouts One way we can implement a request timeout on database calls is to take advantage of Spring's @Transactional annotation. It has a timeout property that we can set. The default value for this property is -1, which is equivalent to not having any timeout at all.

What is default connection timeout in spring boot?

connection-timeout=5000 in your application. properties. From the official documentation: server. connection-timeout= # Time in milliseconds that connectors will wait for another HTTP request before closing the connection.

What is spring datasource Hikari connection timeout?

spring.datasource.hikari.connection-timeout=60000. Controls the maximum number of milliseconds that you will wait for setting up a connection from the pool. spring.datasource.hikari.idle-timeout=600000. Controls the maximum amount of time that a connection is allowed to sit idle in the pool.

How do I increase timeout in REST API?

The REST client timeouts when a REST call takes longer to respond than the specified time. When this happens, the OUT event fails. The default value of the REST client response timeout is 120 seconds. You can increase this time if an adapter that you use has longer than normal response times.


1 Answers

There are at least 3 time-outs to configure:

  1. Transaction timeouts, which you already did. I declared mine in the transactionManager bean:
txManager.setDefaultTimeout(myDefaultValue);
  1. Query timeouts(which obviously does not need @transactional), which you already did and also explained here

  2. Network timeouts(Read this excellent article).

For my case, i am using Oracle, and my bean configuration is as follows:

    @Bean
    public HikariDataSource dataSource() {
        
        HikariDataSource ds = new HikariDataSource();
        ds.setDriverClassName(springDatasourceDriverClassName);
        ds.setJdbcUrl(springDatasourceUrl);
        ds.setUsername(springDatasourceUsername);
        ds.setPassword(springDatasourcePassword);
        ds.setDataSourceProperties(oracleProperties());
        
        return ds;
    }
    
    Properties oracleProperties() {
        Properties properties = new Properties();
        
        properties.put("oracle.net.CONNECT_TIMEOUT", 10000);
        properties.put("oracle.net.READ_TIMEOUT", 10000);
        properties.put("oracle.jdbc.ReadTimeout", 10000);

        return properties;
    }

And if you do not want to configure a bean for the DataSource(which is what most people will do), you can configure the network timeout properties in application.properties:

spring.datasource.hikari.data-source-properties.oracle.net.CONNECT_TIMEOUT=10000
spring.datasource.hikari.data-source-properties.oracle.net.READ_TIMEOUT=10000
spring.datasource.hikari.data-source-properties.oracle.jdbc.ReadTimeout=10000
like image 81
jumping_monkey Avatar answered Sep 23 '22 06:09

jumping_monkey