HiKariCP是数据库连接池的一个后起之秀,号称性能最好,可以完美地PK掉其他连接池
官网:/brettwooldridge/HikariCP
Java 8 maven artifact:
<dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId><version>2.6.2</version></dependency>
Java 9 Early Access maven artifact:
<dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP-java9ea</artifactId><version>2.6.1</version></dependency>
Java 7 maven artifact (maintenance mode):
<dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP-java7</artifactId><version>2.4.12</version></dependency>
Java 6 maven artifact (maintenance mode):
<dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP-java6</artifactId><version>2.3.13</version></dependency
Initialization
You can use theHikariConfig
class like so1:
HikariConfig config = new HikariConfig();config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");config.setUsername("bart");config.setPassword("51mp50n");config.addDataSourceProperty("cachePrepStmts", "true");config.addDataSourceProperty("prepStmtCacheSize", "250");config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");HikariDataSource ds = new HikariDataSource(config);
1MySQL-specific example, do not copy verbatim.
or directly instantiate aHikariDataSource
like so:
HikariDataSource ds = new HikariDataSource();ds.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");ds.setUsername("bart");ds.setPassword("51mp50n");...
or property file based:
// Examines both filesystem and classpath for .properties fileHikariConfig config = new HikariConfig("/some/path/hikari.properties");HikariDataSource ds = new HikariDataSource(config);
Example property file:
dataSourceClassName=org.postgresql.ds.PGSimpleDataSourcedataSource.user=testdataSource.password=testdataSource.databaseName=mydbdataSource.portNumber=5432dataSource.serverName=localhost
orjava.util.Properties
based:
Properties props = new Properties();props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");props.setProperty("dataSource.user", "test");props.setProperty("dataSource.password", "test");props.setProperty("dataSource.databaseName", "mydb");props.put("dataSource.logWriter", new PrintWriter(System.out));HikariConfig config = new HikariConfig(props);HikariDataSource ds = new HikariDataSource(config);
There is also a System property available,hikaricp.configurationFile
, that can be used to specify the location of a properties file. If you intend to use this option, construct aHikariConfig
orHikariDataSource
instance using the default constructor and the properties file will be loaded.
Spring配置文件
<!-- Hikari Datasource -->
<bean id="dataSourceHikari" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="shutdown">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 连接只读数据库时配置为true, 保证安全 -->
<property name="readOnly" value="false" />
<!-- 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->
<property name="connectionTimeout" value="30000" />
<!-- 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->
<property name="idleTimeout" value="600000" />
<!-- 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL
wait_timeout参数(show variables like '%timeout%';) -->
<property name="maxLifetime" value="1800000" />
<!-- 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->
<property name="maximumPoolSize" value="60" />
<property name="minimumIdle" value="10" />
</bean>
HikariCP所做的一些优化,总结如下:
字节码精简:优化代码,直到编译后的字节码最少,这样,CPU缓存可以加载更多的程序代码;优化代理和拦截器:减少代码,例如HikariCP的Statement proxy只有100行代码,只有BoneCP的十分之一;自定义数组类型(FastStatementList)代替ArrayList:避免每次get()调用都要进行range check,避免调用remove()时的从头到尾的扫描;自定义集合类型(ConcurrentBag):提高并发读写的效率;其他针对BoneCP缺陷的优化,比如对于耗时超过一个CPU时间片的方法调用的研究(但没说具体怎么优化)。