JDBC Connection Pooling with Hikari CP & MS SQL Server |Video upload date:  · Duration: PT4M21S  · Language: EN

Compact guide to set up Hikari CP for JDBC pooling with Microsoft SQL Server covering config best practices tuning and common pitfalls

If your Java app treats database connections like disposable party favors you will hit trouble. HikariCP gives you a sane JDBC connection pool that actually behaves and does not pretend to be magic. This guide shows how to wire HikariCP up to Microsoft SQL Server and keep the database from staging a revolt.

Why use HikariCP for SQL Server

Short answer: it is fast, light, and predictable. Long answer: HikariCP minimizes overhead when acquiring and releasing JDBC connections and exposes the knobs you need to tune actual throughput. That matters more than creating new connections on demand like it is 1999.

Step 1 Add the dependencies

Make sure the runtime has both the Microsoft JDBC driver and HikariCP. For Maven add dependency entries to your pom.xml. For Gradle add implementation lines to your build file.

<!-- Maven examples -->
<dependency>
  <groupId>com.zaxxer</groupId>
  <artifactId>HikariCP</artifactId>
  <version>5.0.1</version>
</dependency>

<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>12.2.0.jre11</version>
</dependency>

# Gradle example
# implementation 'com.zaxxer:HikariCP:5.0.1'
# implementation 'com.microsoft.sqlserver:mssql-jdbc:12.2.0.jre11'

Step 2 Create and configure HikariDataSource

Use HikariConfig or create a HikariDataSource directly. Point it at the Microsoft DataSource implementation for best compatibility and avoid repeatedly composing long JDBC URLs in code.

// Java example
HikariConfig cfg = new HikariConfig();
cfg.setDataSourceClassName("com.microsoft.sqlserver.jdbc.SQLServerDataSource");
cfg.addDataSourceProperty("user", "yourUser");
cfg.addDataSourceProperty("password", "yourPassword");
cfg.addDataSourceProperty("serverName", "db-host");
cfg.addDataSourceProperty("databaseName", "mydb");
cfg.setMaximumPoolSize(20);
cfg.setConnectionTimeout(30000);
cfg.setIdleTimeout(600000);

HikariDataSource ds = new HikariDataSource(cfg);

Bind the DataSource into your DAOs so they call ds.getConnection() instead of new DriverManager calls. Frameworks such as Spring Boot will accept a HikariDataSource bean and wire the pool automatically. If you wire things manually remember to close the DataSource on shutdown to avoid orphaned sockets.

Step 3 Spring Boot notes

Spring Boot uses HikariCP by default when it finds the dependency. Typical properties go into application.properties or application.yml and are sane defaults. Override them when your workload needs it.

# application.properties example
spring.datasource.url=jdbc:sqlserver://db-host;databaseName=mydb
spring.datasource.username=yourUser
spring.datasource.password=yourPassword
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.connection-timeout=30000

Tuning tips that do not require faith

  • Set maximumPoolSize to the number of concurrent DB threads you expect plus a small safety margin.
  • Adjust connectionTimeout and idleTimeout based on real latency measurements, not gut feelings.
  • Too many connections will slam the SQL Server and slow everything down, so less can be more.
  • If connection acquisition failures show up in logs you are likely hitting pool or DB limits, not mysterious app karma.

Quick rules of thumb

  • If your CPU on the DB server is pegged lower the pool size and optimize queries and indexes.
  • If threads are frequently queuing increase pool size a little and monitor.
  • Watch latency not connection counts as your single source of truth.

Monitoring and diagnostics

Expose metrics with Micrometer or JMX so you can see active connections idle connections and waiting threads. HikariCP exposes useful MBeans and Meter bindings that integrate with most observability stacks. Logs often tell the story when acquisition times spike or connections time out.

Summary and parting advice

Steps in one line for those who skim and pretend they read it

  • Add driver and HikariCP
  • Configure HikariDataSource with SQL Server DataSource class name and credentials
  • Inject the pool into your code or let Spring Boot do it
  • Tune maximumPoolSize connectionTimeout and idleTimeout based on metrics
  • Monitor and close the DataSource on shutdown

Remember the goal is predictable latency and efficient database resource use not heroic connection creation on demand. If the database is the bottleneck you will get better returns from query tuning and indexing than from cranking up the pool size and hoping for the best.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.