【Java】DB Connection Pool ~ HikariCP / 導入編 ~

■ 公式サイト

http://brettwooldridge.github.io/HikariCP/

Requirements

 * Java 6 and above
 * slf4j library

■ 環境設定

 * Gradle を使用
 * 以下の関連記事の「準備 : Buildship: Eclipse Plug-ins for Gradle のインストール」を行い
   Gradle プロジェクトを作成し、Eclipseの対象プロジェクトを右クリックし、
   [Gradle]-[Refresh Gradle Project]を選択しモジュールをダウンロード
https://blogs.yahoo.co.jp/dk521123/37204914.html

build.gradle

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // !! HikariCP for Java8/9 !!
    compile group: 'com.zaxxer', name: 'HikariCP', version: '2.7.7'
    testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
    
    // MySQL
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.13'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

■ サンプル

DataSource.java

import java.io.Closeable;

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

public class DataSource implements Closeable {
  private HikariDataSource dataSource;

  public DataSource() {
    HikariConfig config = new HikariConfig();

    // MySQL用ドライバを設定
    config.setDriverClassName("com.mysql.jdbc.Driver");

    // URL指定
    config.setJdbcUrl("jdbc:mysql://localhost:3306/sampledb?useSSL=false");

    // ユーザ名、パスワード指定
    config.addDataSourceProperty("user", "root");
    config.addDataSourceProperty("password", "password");

    // キャッシュ系の設定(任意)
    config.addDataSourceProperty("cachePrepStmts", "true");
    config.addDataSourceProperty("prepStmtCacheSize", "250");
    config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    // サーバサイドプリペアードステートメントを使用する(任意)
    config.addDataSourceProperty("useServerPrepStmts", "true");

    // エンコーディング
    config.addDataSourceProperty("characterEncoding", "utf8");

    // 接続をテストするためのクエリ
    config.setConnectionInitSql("SELECT 1");

    // 接続
    this.dataSource = new HikariDataSource(config);
  }

  @Override
  public void close() {
    if (this.dataSource != null) {
      this.dataSource.close();
    }
  }
  
  public HikariDataSource getDataSource() {
    return this.dataSource;
  }
}

Main.java

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

public class Main {
  public static void main(String[] args) {

    // ★Close処理については、https://blogs.yahoo.co.jp/dk521123/37513909.html を参照★
    try (DbDataSource dataSource = new DbDataSource();
        Connection connection = dataSource.getDataSource().getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from person");) {
      while (resultSet.next()) {
        System.out.println("Id:" + resultSet.getString("id") + " Name:"
            + resultSet.getString("name"));
      }
    } catch (SQLException ex) {
      ex.printStackTrace();
    }
  }
}

■ パフォーマンス検証

HikariCPの検証プログラム

HikariCpDemo.java
import java.sql.Connection;
import java.sql.SQLException;

public class HikariCpDemo {
  public static void main(String[] args) {
    DataSource dataSource = new DataSource();

    long start = System.currentTimeMillis();
    for (int i = 0; i < 101; i++) {
      long lap = System.currentTimeMillis();
      try (Connection connection = dataSource.getDataSource().getConnection();) {
      } catch (SQLException ex) {
        ex.printStackTrace();
      }
      System.out.println("[" + i + "]" + (System.currentTimeMillis() - lap));
    }
    long end = System.currentTimeMillis();
    System.out.println("[Done]" + (end - start));

    dataSource.close();
  }
}

検証結果

まず、以下の関連記事で行った検証結果。
https://blogs.yahoo.co.jp/dk521123/37305626.html
【1】Poolなしの検証プログラム
1回目:769ms(うち初回接続:395ms)
2回目:744ms(うち初回接続:379ms)
3回目:750ms(うち初回接続:390ms)
【2】 Tomcat JDBC Connection Poolの検証プログラム
1回目:397ms(うち初回接続:374ms)
2回目:415ms(うち初回接続:392ms)
3回目:396ms(うち初回接続:373ms)
★今回★ HikariCPの検証プログラム
1回目:32ms(うち初回接続:21ms)
2回目:45ms(うち初回接続:20ms)
3回目:26ms(うち初回接続:17ms)

 => 圧倒的に速い!!

■ その他事項

 * 「使用上の注意」などの以下の関連記事を参照のこと。
https://blogs.yahoo.co.jp/dk521123/37513909.html

■ 複数DBでの扱い

 * 以下の関連記事を参照のこと。
https://blogs.yahoo.co.jp/dk521123/37452804.html


参考文献

他のDB Connection Poolにも言及
http://hatappo.hatenadiary.jp/entry/2015/08/09/200037
プロパティに関する説明が詳しい
http://time-complexity.blogspot.jp/2015/03/db-connection-pool-hikaricp.html
シンプルなサンプルが掲載
https://jyn.jp/java-hikaricp-mysql-sqlite/

関連記事

Java】DB Connection Pool ~ HikariCP / 基本編 ~

https://blogs.yahoo.co.jp/dk521123/37513909.html

Java】DB Connection Pool ~ HikariCP / 複数DB編 ~

https://blogs.yahoo.co.jp/dk521123/37452804.html

Java】DB Connection Pool ~ Tomcat JDBC Connection Pool 編 ~

https://blogs.yahoo.co.jp/dk521123/37305626.html