1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HikariCPとcom.mysql.jdbc.ReplicationDriverを併用する

Last updated at Posted at 2016-03-01

前置き

関連するjarのバージョンは以下

  • HikariCP (2.4.1)
  • mysql-connector-java (5.1.36)

使い方

HikariDataSourceのsetDriverClassNameでcom.mysql.jdbc.ReplicationDriverを指定すれば良い。

HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setDriverClassName("com.mysql.jdbc.ReplicationDriver");

その他オプション(username, password, etc)もHikariDataSourceに指定可能。

使ってみて気になったこと

これでアプリケーションを作成して、起動すると、以下の様なログが出ると思う。

2016-03-01 11:11:35 WARN DriverDataSource - Registered driver with driverClassName=com.mysql.jdbc.ReplicationDriver was not found, trying direct instantiation. (DriverDataSource.java:68)

ちゃんとReplicationDriver使われてるのか気になったのでちょっと調べてみた。
以下はそのメモ。

調べたこと

ログに記載されてるDriverDataSourceの該当箇所はこんな感じ。

DriverDataSource.java
if (driverClassName != null) {
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver d = drivers.nextElement();
        if (d.getClass().getName().equals(driverClassName)) {
            driver = d;
            break;
        }
    }

    if (driver == null) {
        LOGGER.warn("Registered driver with driverClassName={} was not found, trying direct instantiation.", driverClassName);
        try {
            Class<?> driverClass = this.getClass().getClassLoader().loadClass(driverClassName);
            driver = (Driver) driverClass.newInstance();
        }
        catch (Exception e) {
            LOGGER.warn("Could not instantiate instance of driver class {}, trying JDBC URL resolution", driverClassName, e);
        }
    }
}

DriverManagerから取得したDriverの一覧に期待するものが無ければ、クラス情報からnewInstanceする感じ。

ここで、MySQL側の公式ドキュメントを見ると、

To enable this functionality, use the com.mysql.jdbc.ReplicationDriver class when configuring your application server's connection pool or when creating an instance of a JDBC driver for your standalone application. Because it accepts the same URL format as the standard MySQL JDBC driver, ReplicationDriver does not currently work with java.sql.DriverManager-based connection creation unless it is the only MySQL JDBC driver registered with the DriverManager .

と、ある。java.sql.DriverManagerベースでは動かないと。。

なので先のwarnのログが出るのはソースからも分かるし、その後newInstanceでReplicationDriverが生成されて利用できるので、動作に問題は無さそう。
なおHikariCPのこの辺りの挙動は、2.3.6, 2.3.7で修正が入っている模様。

一旦使ってみてまた何かあったら記録しよう。

cf.
http://stackoverflow.com/questions/29391693/squeryl-hikaricp-mysql-distributing-read-traffic-to-slaves
https://github.com/brettwooldridge/HikariCP/blob/dev/CHANGES

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?