快速,简单,可靠。HikariCP是一个“零开销”生产就绪JDBC连接池。大约130Kb,它的性能几乎是C3P0、DBCP的25倍,十分强悍
引用大话数据库连接池
配置也是十分简单
- maven依赖
Java 8/9 maven artifact:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.1.0</version>
</dependency>
Java 7 maven artifact (maintenance mode):
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
<version>2.4.13</version>
</dependency>
Java 6 maven artifact (maintenance mode):
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
<version>2.3.13</version>
</dependency>
- 创建hikari.properties文件,放到src下或resources下
jdbcUrl=jdbc:mysql://xxx:3306/testdb?useSSL=false&characterEncoding=utf-8
dataSource.user=root
dataSource.password=root
#连接的闲置时间
dataSource.maxLifetime=604800
#连接池大小
dataSource.maximumPoolSize=18
dataSource.cachePrepStmts = true
dataSource.prepStmtCacheSize = 250
dataSource.prepStmtCacheSqlLimit = 2048
dataSource.useServerPrepStmts = true
dataSource.useLocalSessionState = true
dataSource.rewriteBatchedStatements = true
dataSource.cacheResultSetMetadata = true
dataSource.cacheServerConfiguration = true
dataSource.elideSetAutoCommits = true
dataSource.maintainTimeStats = false
其实只需根据情况配置以下两项即可,其它都可以用默认
dataSource.maxLifetime=604800
dataSource.maximumPoolSize=18
- 代码如下
HikariConfig config = new HikariConfig("/hikari.properties");
HikariDataSource ds = new HikariDataSource(config);
Connection conn = ds.getConnection();