Check how to set the timeout when connecting with Spring + HikariCP + MySQL and executing SQL.
An environment that takes action
Spring relations: 5.0.9 HikariCP:2.7.9 MySQL:5.7
Set with setConnectionTimeout () for the instance of HikariDatasource.
@Bean
HikariDataSource datasource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false&socketTimeout=10");
ds.setConnectionTimeout(250); //★ Here
ds.setUsername("user");
ds.setPassword("password");
return ds;
}
↓ setting of application.properties
spring.datasource.hikari.connection-timeout=Timeout (milliseconds)
Add the socketTimeout parameter to the JDBC URL.
@Bean
HikariDataSource datasource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false&socketTimeout=10"); //★ Here
ds.setConnectionTimeout(250);
ds.setUsername("user");
ds.setPassword("password");
return ds;
}
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&socketTimeout=Timeout (milliseconds)
See ↓ for how to reproduce network delay in windows environment. How to reproduce network delay locally in windows
The code written as a sample is Connection timeout: 250ms Communication timeout after connection: 10ms Therefore, check that the exception appears as expected by following the procedure below.
Below are the steps.
Looking at the execution time of the log, the exception log does not appear according to the specified millisecond. I think that the timeout setting value and the delay setting value time out as intended, so I think that the timeout judgment itself can be done, but ... A mystery.
Recommended Posts