HikariCP Java Tutorial: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

HikariCP Java: Complete Guide with Examples

1. What Is HikariCP and Why Use It?

HikariCP (pronounced "hee-ka-ree-cee-pee") is a high-performance JDBC connection pool for Java. The name comes from the Japanese word "光" (hikari), meaning "light." This library is built with a laser focus on minimal latency and maximum throughput. While Java has several connection pools (like Apache DBCP, C3P0, and Tomcat JDBC), HikariCP is widely recognized as the fastest and lightest option available.

Why do you need a connection pool? Establishing a database connection is expensive — it involves a TCP handshake, authentication, and session setup. Opening a new connection for every SQL query would cripple your application's performance. A connection pool solves this by maintaining a "reservoir" of pre-created connections. When your app needs to run a query, it grabs a ready-to-use connection from the pool, uses it, and returns it. This dramatically speeds up operations and reduces database load.

HikariCP powers major projects like Spring Boot (default since version 2.0), Play Framework, Dropwizard, and many others. It supports all major databases: PostgreSQL, MySQL, MariaDB, Oracle, H2, HSQLDB, SQL Server, and more. Thanks to its efficiency and simplicity, HikariCP has become the de facto standard for connection pooling in the Java ecosystem.

2. Installation

HikariCP is distributed through the Maven Central Repository. To install it, add the dependency to your pom.xml (for Maven) or build.gradle (for Gradle). You'll also need the JDBC driver for your specific database.

Maven (pom.xml)

<!-- HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>5.1.0</version>
</dependency>

<!-- JDBC driver for PostgreSQL (example) -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.7.1</version>
</dependency>

Gradle (build.gradle)

implementation 'com.zaxxer:HikariCP:5.1.0'
implementation 'org.postgresql:postgresql:42.7.1'

Important: Versions may change. Always check the latest version on the official Maven Central website. For other databases, replace the driver with the appropriate one (e.g., mysql-connector-j for MySQL).

3. Quick Start — Minimal Working Example

Below is a minimal example of using HikariCP to run a simple query against a PostgreSQL database. This assumes you have PostgreSQL running with a database called testdb and a users table.

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class QuickStart {
    public static void main(String[] args) {
        // 1. Configure the pool
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:postgresql://localhost:5432/testdb");
        config.setUsername("postgres");
        config.setPassword("password");
        config.setMaximumPoolSize(10);

        // 2. Create the DataSource
        try (HikariDataSource dataSource = new HikariDataSource(config)) {

            // 3. Get a connection from the pool
            try (Connection                    

Recommendations