コネクションPOOL に HikariCP を使用した mybatis の接続
【再興】mybatis XML設定を使わない場合 - Oboe吹きプログラマの黙示録 のパターンで書いてみる。
HikariCP 3.4.5
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency>
gradle の場合、
dependencies { implementation 'mysql:mysql-connector-java:8.0.21' implementation 'com.zaxxer:HikariCP:3.4.5' }
サンプル
HikariDataSource に接続設定をセットして SqlSessionFactoryBuilder → SqlSessionFactory
public static SqlSession getSqlSession(Class<?> mapperclass){ HikariDataSource dataSource = new HikariDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/testDB?serverTimezone=JST"); dataSource.setUsername("root"); dataSource.setPassword("pass"); dataSource.setAutoCommit(false); dataSource.setMaximumPoolSize(10); dataSource.setMinimumIdle(4); dataSource.setConnectionTestQuery("SELECT 1"); Environment environment = new Environment("deployment", new JdbcTransactionFactory(), dataSource); Configuration config = new Configuration(environment); // snake Case → camel Case config.setMapUnderscoreToCamelCase(true); config.addMapper(mapperclass); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config); return factory.openSession(); }
auto commit は、false を指定しないと default は true
接続開始時(立ち上げ時)、次のようにコネクションPOOLの状況を
標準出力にログを出してくれるので設定を確認できる。
22:57:18.285 [main] DEBUG com.zaxxer.hikari.HikariConfig - Driver class com.mysql.cj.jdbc.Driver found in Thread context class loader sun.misc.Launcher$AppClassLoader@73d16e93 22:57:18.436 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. 22:57:18.460 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection 22:57:18.460 [main] DEBUG com.zaxxer.hikari.HikariConfig - HikariPool-1 - configuration: 22:57:18.465 [main] DEBUG com.zaxxer.hikari.HikariConfig - allowPoolSuspension.............false 22:57:18.465 [main] DEBUG com.zaxxer.hikari.HikariConfig - autoCommit......................false 22:57:18.465 [main] DEBUG com.zaxxer.hikari.HikariConfig - catalog.........................none 22:57:18.465 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionInitSql...............none 22:57:18.465 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTestQuery............."SELECT 1" 22:57:18.466 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTimeout...............30000 22:57:18.466 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSource......................none 22:57:18.466 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceClassName.............none 22:57:18.466 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceJNDI..................none 22:57:18.467 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceProperties............{password=<masked>} 22:57:18.467 [main] DEBUG com.zaxxer.hikari.HikariConfig - driverClassName................."com.mysql.cj.jdbc.Driver" 22:57:18.467 [main] DEBUG com.zaxxer.hikari.HikariConfig - exceptionOverrideClassName......none 22:57:18.467 [main] DEBUG com.zaxxer.hikari.HikariConfig - healthCheckProperties...........{} 22:57:18.467 [main] DEBUG com.zaxxer.hikari.HikariConfig - healthCheckRegistry.............none 22:57:18.467 [main] DEBUG com.zaxxer.hikari.HikariConfig - idleTimeout.....................600000 22:57:18.467 [main] DEBUG com.zaxxer.hikari.HikariConfig - initializationFailTimeout.......1 22:57:18.467 [main] DEBUG com.zaxxer.hikari.HikariConfig - isolateInternalQueries..........false 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - jdbcUrl.........................jdbc:mysql://localhost:3306/testDB?serverTimezone=JST 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - leakDetectionThreshold..........0 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - maxLifetime.....................1800000 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - maximumPoolSize.................10 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - metricRegistry..................none 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - metricsTrackerFactory...........none 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - minimumIdle.....................4 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - password........................<masked> 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - poolName........................"HikariPool-1" 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - readOnly........................false 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - registerMbeans..................false 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - scheduledExecutor...............none 22:57:18.468 [main] DEBUG com.zaxxer.hikari.HikariConfig - schema..........................none 22:57:18.469 [main] DEBUG com.zaxxer.hikari.HikariConfig - threadFactory...................internal 22:57:18.469 [main] DEBUG com.zaxxer.hikari.HikariConfig - transactionIsolation............default 22:57:18.469 [main] DEBUG com.zaxxer.hikari.HikariConfig - username........................"root" 22:57:18.469 [main] DEBUG com.zaxxer.hikari.HikariConfig - validationTimeout...............5000 22:57:18.469 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting... 22:57:18.693 [main] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@51931956 22:57:18.695 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.