Java.HikariCP.How does HikariCP validate that a connection is alive before giving it to the application?

HikariCP validates a connection before handing it to your application using a connection test query or JDBC-standard connection validation methods, depending on your configuration:

🔹 1. Connection test query (connectionTestQuery)
You can explicitly set a lightweight SQL statement (e.g., SELECT 1) that HikariCP will execute to check if the connection is still valid before giving it out.

Example:

spring.datasource.hikari.connection-test-query=SELECT 1

🔹 2. JDBC isValid() method (preferred)
By default, HikariCP uses the JDBC Connection.isValid(timeout) method to validate a connection if no connectionTestQuery is provided.

This is more efficient than a query for modern drivers because it avoids unnecessary SQL statements.

The validationTimeout setting controls how long HikariCP waits for a response during this check.

🔹 3. When validation happens
HikariCP doesn’t validate idle connections continuously by default — instead, it validates them just before handing them out to the application, minimizing overhead.

🔹 4. Configuration example for validationTimeout

spring.datasource.hikari.validation-timeout=5000  # 5 seconds

Key point:
Using Connection.isValid() is usually faster and more efficient, but if your driver doesn’t support it properly, falling back to a simple connectionTestQuery is a reliable alternative.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.