August 18, 2022

HikariCP Connection Pooling Java Example

In this post we’ll see how to configure connection pooling using HikariCP in your JDBC application.

Maven dependency for HikariCP

If you are using Maven then you can add the following dependency in pom.xml which works for Java 8 to Java 11.


You can also download jars from here-

HikariCP Connection pooling Java example

In the example DB used is MySQL it connects to knpcode schema and table is EMPLOYEE with columns as id, FIRST_NAME, LAST_NAME and DEPARTMENT.

Best way to create a HIkariCP DataSource is to instantiate an instance of HikariConfig class and provide the properties for connecting to DB and for connection pool. Then pass that config instance to create HikariDataSource object.

DB related configurations that you need to set are Driver class, URL, user name and password.

Some of the connection pool related properties that you can set are as follows-

  • autoCommit- This property controls the default auto-commit behavior of connections returned from the pool. Default is true.
  • connectionTimeout- This property controls the maximum number of milliseconds that a client will wait for a connection from the pool. Default is 30 seconds.
  • idleTimeout- This property controls the maximum amount of time that a connection is allowed to sit idle in the pool. Default is 10 minutes.
  • maxLifetime- This property controls the maximum lifetime of a connection in the pool. Default is 30 minutes.
  • maximumPoolSize- This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections. Default is 10.

For the example DB credentials and connection pool related configuration properties are kept in a properties file (


Following class is used to create a HikariDataSource by setting DB properties and connection pool properties which are read from the properties file.

import java.util.Properties;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class DSCreator {
  private static HikariDataSource ds;
  static {
    try {
      Properties properties = new Properties();
      // Loading properties file from classpath
      InputStream inputStream = DSCreator.class
      if(inputStream == null){
        throw new IOException("File not found");
      HikariConfig config = new HikariConfig();
      // This property is optional now 
      config.addDataSourceProperty("cachePrepStmts", "true");
      config.addDataSourceProperty("prepStmtCacheSize", "250");
      config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
      // Create DataSource
      ds = new HikariDataSource(config);
    }catch(IOException e) {
  public static DataSource getDataSource() {
    return ds;

Test class that creates a connection using the returned DataSource object and queries the DB.

public class DSDemo {
  public static void main(String[] args) {
    DSDemo dsDemo = new DSDemo();
  private void displayEmployeeById(int id){
    Connection connection = null; 
    String selectSQL = "SELECT * FROM EMPLOYEE WHERE id = ?";
    PreparedStatement prepStmt = null;
    try {
      DataSource ds = DSCreator.getDataSource();
      connection = ds.getConnection();
      prepStmt = connection.prepareStatement(selectSQL);
      prepStmt.setInt(1, id);
      ResultSet rs = prepStmt.executeQuery();
        System.out.println("id: " + rs.getInt("id"));
        System.out.println("First Name: " + rs.getString("FIRST_NAME"));
        System.out.println("Last Name: " + rs.getString("LAST_NAME"));
        System.out.println("Department: " + rs.getString("DEPARTMENT"));
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      if(connection != null){
        try {
        } catch (SQLException e) {
          // TODO Auto-generated catch block

That's all for the topic HikariCP Connection Pooling Java Example. If something is missing or you have something to share about the topic please write a comment.

You may also like

No comments:

Post a Comment