Java
javaTutorial
Database query timeout and connection pool configuration practice in Spring Boot
Database query timeout and connection pool configuration practice in Spring Boot

Understand the limitations of Spring @Transactional timeout mechanism
In Spring Boot applications, developers often use the @Transactional annotation to manage transactions and set the transaction timeout through its timeout attribute. For example, the following code snippet shows the common practice of setting transaction timeout to 5 seconds:
@GetMapping("/return")
@Transactional(timeout = 5)
public List<testentity> findAll() throws InterruptedException {
return testRepository.findAll();
}</testentity>
However, this configuration is often ineffective in interrupting ongoing database query operations. The timeout mechanism of @Transactional(timeout) is checked when the transaction is committed or rolled back. This means that if a database query (such as testRepository.findAll()) itself takes longer than the set 5 seconds, the Spring transaction manager will not interrupt it during the query execution. It waits for the database query to be completely executed before checking whether the entire transaction has timed out. If the query takes 15 seconds, the transaction timeout exception will only be thrown after 15 seconds, rather than immediately interrupting at 5 seconds.
The fundamental reason for this behavior is that the timeout of the @Transactional annotation is for the entire transaction logic unit. It cannot directly intervene in the IO operation between the underlying JDBC driver and the database, nor can it forcefully interrupt a SQL statement that has been sent to the database and is being executed. To achieve more fine-grained timeout control, especially for the timeout of the database connection or the query itself, we need to use the configuration of the connection pool.
Timeout control through HikariCP connection pool configuration
In order to more effectively solve the problem of long-term unresponsiveness of database queries and ensure that the application can throw exceptions in time when the database operation is stuck, we can use the configuration of Spring Boot's default database connection pool HikariCP. HikariCP provides a key parameter: connection-timeout.
The spring.datasource.hikari.connection-timeout parameter defines the maximum wait time (in milliseconds) for the client to obtain a connection from the connection pool. When an application attempts to obtain a connection from the connection pool, but there are no connections immediately available in the pool (for example, all connections are occupied, or the database is slow to respond causing a delay in connection release), HikariCP waits for a period of time. If the waiting time exceeds the value set by connection-timeout, HikariCP will throw a SQLException indicating that the connection cannot be obtained.
Although connection-timeout is mainly used to control the acquisition of connections, it plays an important role in solving application hang problems caused by long-time database operations. A database query that has not been completed for a long time will continue to occupy a connection, making it impossible to release the connection back to the connection pool. When all connections in the connection pool are occupied by similar long queries, any subsequent attempts to obtain a connection will be limited by the connection-timeout. If these subsequent operations cannot obtain a connection within the specified time, they will fail immediately and throw an exception, preventing the application from blocking indefinitely waiting for a database response. This indirectly enables applications to detect response problems in database operations more quickly and handle errors accordingly.
Configuration example:
Add the following configuration in the application.yml file to set the connection timeout to 5000 milliseconds (i.e. 5 seconds):
spring:
datasource:
hikari:
connection-timeout: 5000 #Set the connection timeout to 5 seconds
With this configuration, if the application cannot get an available database connection from the HikariCP connection pool within 5 seconds, it will throw an exception. This helps ensure that even if a long query blocks the connection, the application does not wait indefinitely, thereby improving the overall stability and responsiveness of the system.
Configuration considerations and best practices
- Unit and value setting: The unit of connection-timeout is milliseconds. Setting this value appropriately is critical. If the setting is too short, normal but slightly slower queries may fail due to the inability to obtain a connection; if the setting is too long, database performance problems may not be discovered in time, causing the application to be blocked for a long time. It is recommended to adjust based on actual business needs and database performance baseline.
- Global impact: connection-timeout is a global configuration at the connection pool level, which will affect the connection acquisition behavior of all database operations through this data source. Therefore, when adjusting this parameter, the impact on the entire application needs to be considered.
- Distinguish between different types of timeouts: It is important to understand the difference between connection-timeout and @Transactional(timeout) and possibly socketTimeout or queryTimeout at the JDBC driver level.
- @Transactional(timeout): Timeout at the transaction level, checked after the transaction ends.
- spring.datasource.hikari.connection-timeout: Timeout at the connection pool level, controlling the maximum waiting time for obtaining a connection.
- Timeouts at the JDBC driver or database statement level: These are usually used to directly control the execution time of a single SQL query, such as through the Statement.setQueryTimeout() method, or by configuring parameters such as socketTimeout in the JDBC connection URL. These settings enable more direct interruption of executing SQL queries. However, the focus of this tutorial is based on the solution provided, namely HikariCP's connection-timeout.
Summarize
Although Spring's @Transactional(timeout) annotation is very useful in controlling the execution time of the entire transaction, it cannot directly interrupt the executing database query. In order to effectively prevent the application from hanging due to long-time database operations and ensure that exceptions can be thrown in time during the database connection acquisition phase, configuring the spring.datasource.hikari.connection-timeout parameter of the HikariCP connection pool is a simple and effective solution. By setting this parameter appropriately, Spring Boot applications can better manage database resources and improve robustness and user experience when facing slow database response.
The above is the detailed content of Database query timeout and connection pool configuration practice in Spring Boot. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20518
7
13631
4
How to configure Spark distributed computing environment in Java_Java big data processing
Mar 09, 2026 pm 08:45 PM
Spark cannot run in local mode, ClassNotFoundException: org.apache.spark.sql.SparkSession. This is the most common first step of getting stuck: even the dependencies are not correct. Only spark-core_2.12 is written in Maven, but spark-sql_2.12 is not added. SparkSession crashes as soon as it is built. The Scala version must strictly match the official Spark compiled version - Spark3.4.x uses Scala2.12 by default. If you use spark-sqljar of 2.13, the class loader cannot directly find the main class. Practical advice: Go to mvnre
How to safely map user-entered weekday string to integer value and implement date offset operation in Java
Mar 09, 2026 pm 09:43 PM
This article introduces a concise and maintainable way to map the weekday string (such as "Monday") to the corresponding serial number (1-7), and use the modulo operation to realize the forward and backward offset of any number of days (such as Monday plus 4 days to get Friday), avoiding lengthy if chains and hard-coded logic.
How to generate a list of duplicate elements using Java's Collections.nCopies_Initialization tips
Mar 06, 2026 am 06:24 AM
Collections.nCopies returns an immutable view. Calling add/remove will throw UnsupportedOperationException; it needs to be wrapped with newArrayList() to modify it, and it is disabled for mutable objects.
How to use Homebrew to install Java on Mac_A must-have Java tool chain for developers
Mar 09, 2026 pm 09:48 PM
Homebrew installs the latest stable version of openjdk (such as JDK22) by default, not the LTS version; you need to explicitly execute brewinstallopenjdk@17 or brewinstallopenjdk@21 to install the LTS version, and manually configure PATH and JAVA_HOME to be correctly recognized by the system and IDE.
What is exception masking (Suppressed Exceptions) in Java_Multiple resource shutdown exception handling
Mar 10, 2026 pm 06:57 PM
What is SuppressedException: It is not "swallowed", but actively archived by the JVM. SuppressedException is not an exception loss, but the JVM quietly attaches the secondary exception to the main exception under the premise that "only one exception must be thrown" for you to verify afterwards. It is automatically triggered by the JVM in only two scenarios: one is that the resource closure in try-with-resources fails, and the other is that you manually call addSuppressed() in finally. The key difference is: the former is fully automatic and safe; the latter requires you to keep it to yourself, and it can be written as shadowing if you are not careful. try-
How to correctly implement runtime file writing in Java applications (avoiding JAR internal write failures)
Mar 09, 2026 pm 07:57 PM
After a Java application is packaged as a JAR, data cannot be written directly to the resources in the JAR package (such as test.txt) because the JAR is essentially a read-only ZIP archive; the correct approach is to write variable data to an external path (such as a user directory, a temporary directory, or a configuration-specified path).
What is the underlying principle of array expansion in Java_Java memory dynamic adjustment analysis
Mar 09, 2026 pm 09:45 PM
ArrayList.add() triggers expansion because grow() is called when size is equal to elementData.length. The first add allocates 10 capacity, and subsequent expansion is 1.5 times and not less than the minimum requirement, relying on delayed initialization and System.arraycopy optimization.
How to safely read a line of integer input in Java and avoid Scanner blocking
Mar 06, 2026 am 06:21 AM
This article introduces typical blocking problems when using Scanner to read multiple integers in a single line. It points out that hasNextInt() will wait indefinitely when there is no subsequent input, and recommends a safe alternative with nextLine() string splitting as the core.





