[JAVA] Check how to set the timeout when connecting with Spring + HikariCP + MySQL and executing SQL

Thing you want to do

Check how to set the timeout when connecting with Spring + HikariCP + MySQL and executing SQL.

environment

An environment that takes action

Spring relations: 5.0.9 HikariCP:2.7.9 MySQL:5.7

Time out when connecting to DB

Items to set (when describing DataSource settings in Java)

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;
}

Items to be set (when using Spring-boot automatic setting)

↓ setting of application.properties

spring.datasource.hikari.connection-timeout=Timeout (milliseconds)

Time out if SQL result is not returned after DB connection

Add the socketTimeout parameter to the JDBC URL.

Items to set (when describing DataSource settings in Java)

@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;
}

Items to be set (when using Spring-boot automatic setting)

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&socketTimeout=Timeout (milliseconds)

Try out

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.

  1. Before the connection is established, if you set the lag to 400ms and execute the process that involves the execution of SQL, you can see the timeout in the output exception. (** Connection timeout **)
  2. Next, stop the occurrence of the delay once and execute the process that involves the execution of SQL (connect the connection pool to the DB).
  3. After that, if you set the lag to 100ms , start the delay, and execute the process that involves the execution of SQL, you can see the timeout in the output exception. (* Timeout when SQL is not returned after connection **)

However,,

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

Check how to set the timeout when connecting with Spring + HikariCP + MySQL and executing SQL
Summary of how to use the proxy set in IE when connecting with Java
How to check before sending a message to the server with Spring Integration
Spring Boot --How to set session timeout time
Settings for connecting to MySQL with Spring Boot + Spring JDBC
How to connect MySQL / MariaDB + HikariCP with Liferay 7 / DXP
How to set the retry limit of sidekiq and notify dead queues with slack
How to set environment variables when using Payjp with Rails
How to set chrony when the time shifts in CentOS7
How to deal with the error ERROR: While executing gem ... (Gem :: FilePermissionError)
How to check the extension and size of uploaded files
Method definition location Summary of how to check When defined in the project and Rails / Gem
Set Spring profile when executing bootRun task with Spring Boot Gradle Plugin
How to set the IP address and host name of CentOS8
[Rails] How to introduce kaminari with Slim and change the design
How to set and use profile in annotation-based Configuration in Spring framework
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
[Note] How to restart the Windows container set up with docker-compose
How to set Spring Boot + PostgreSQL
How to access Socket directly with the TCP function of Spring Integration
How to create a server executable JAR and WAR with Spring gradle
How to deal with FATAL: role "admin0" does not exist and PG :: ConnectionBad: FATAL: role "admin0" does not exist when executing rails db: create
How to set up and use kapt
[Java] How to set the Date time to 00:00:00
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to find the tens and ones
An error occurred when executing a function from MyBatis with the OUT parameter set to CURSOR in PostgreSQL.
How to get the query string to actually issue when using PreparedStatement with JDBC
How to use ToolBar with super margin Part1 Set characters and change colors
How to check the latest version of io.spring.platform to describe in pom.xml of Spring (STS)
How to set when "The constructor Empty () is not visible" occurs in junit
How to set environment variables in the properties file of Spring boot application
How to set up computer vision for tracking images and videos with TrackingJs
How to share on the host side (windows) and guest side (CentOS 7) with VirtualBox
How to check the logs in the Docker container
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
[Swift] How to link the app with Firebase
How to delete the database when recreating the application
[Java] (for MacOS) How to set the classpath
How to set up and operate jEnv (Mac)
How to build API with GraphQL and Rails
How to find the total score and average score
[Spring Boot] How to refer to the property file
How to check Rails commands in the terminal
How to delete the tweet associated with the user when you delete it at the same time
Do you really understand? How to check the library and license used by the app
N things to keep in mind when reading "Introduction to Spring" and "Introduction to Spring" in the Reiwa era
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
How to identify the cause when GC occurs frequently and CPU usage is high